top of page
Search

The backbone of Salesforce: Apex part two sObjects

  • caleksiev
  • Sep 24, 2022
  • 5 min read

Salesforce is a CRM like no other and Apex is a unique programing language as well. As promised ( with some delay ), I would like to introduce you to Salesforce sObjects data type.

What exactly is a sObject? Because there is a tight bond between Apex and the database, we can access Salesforce records and their fields directly from Apex. Every record in our organization is represented as a sObject in Apex. Let's check this example for more clarity: if we have excel spread sheet, containing data, imagine it's data entered like this:

- we have different sheets to organize our data;

- we have different columns names for each entry properties;

- we have rows for each entry's data;

Now, let's check the same thing in Salesforce:

Looks pretty similar, right? So, an sObject is every standard ( that comes out of the box with Salesforce ) object OR a custom object, that we can create in Salesforce. It's as simple as creating a new sheet in excel: a new object, with it's fields and all the different records, that we need. Imagine you have an online education business, you will need for example sObject for Students, for Educators, for Courses and whatever else you can think of. Every sObject will have different fields, for example the Course will have:

- name "Programming with Apex";

- educator "John Smith";

- start date "24-09-2022";

- end date "31-12-2022";

- number of attendees "25";

- is course started "Yes";

And we can keep going to satisfy out online business needs. But, did you noticed that in Salesforce our records ( in our screen example, the Account records ), are clickable, why is that? POWER!!! If you click a record, you are redirected to the particular records data, but let's not talk about that now.

With the knowledge we now have, let's check how can we use sObjects in Apex.

How can we create sObject in Apex? Open up the developer console ( yes, we are still using it, until we get to the point of downloading and setting an IDE like VS Code ) by clicking:

Upper right corner, Setup > Developer Console > Inside the Console Debug > Open Execute Anonymous Window

Inside the console

Now, let's create a new Account object, using the .... Apex "new" keyword:

Account myTestAccount = new Account();

We now have a new Account object which has no data.... an empty row. In order to add some data to it, we must do it by using our variable name, aka myTestAccount, dot notation and API name, like this:

myTestAccount.Name              = 'Test Account';
myTestAccount.AccountNumber     = 'CD736025';
myTestAccount.Industry          = 'Education';
myTestAccount.NumberOfEmployees = 200;

Wait, wait, dot notation, API Name, what?! So, every record ( row ) has fields ( columns name ), which has one name, called Field Label, that is shown to the user and one name, called API name or field name, which is for system usage. While in your account tab ( sheet ), click on any record ( row ) and while in that record, click Setup > Edit Object

You are redirected to that object settings page ( which we will discuss in much more detail a bit later ). On this page, click Fields and relationships on the left, in order to see all the fields of our object:

Leave all the different options on the left for now and let's focus on our things. Check FIELD LABEL ( what we see in the user interface ), FIELD NAME ( or API name, what we use in Apex ) and DATA TYPE of that field. Don't worry if your fields are different than mine.

Upper right, click New

1. select Checkbox data type, click Next

2. Write Field Label "Active", click on the Field Name, it should be auto populated

3. Next ( don't worry about this for now, here we are saying, who can see our new field )

4. Next ( don't worry about this for now, here we are setting, on which layout should our field be on )

5 Save

YEAH! We have our new field set and ready for use! Go back on your account page, refresh and search for your Active checkbox. Now, how can we use our checkbox in Apex. Like this:

myTestAccount.Active__c = true;

If you search for your field in the Fields and relationships options, you will see that in fact the field name is really Active__c, although you did not wrote that. The sufix "__c" is added by Salesforce, showing that this field is Custom, it's the same with custom objects. Let's create one and check it. If you are on accounts tab, click Setup > Setup for current app > Object Manager > Create button > Custom object

Let's call our object vehicle:

Leave everything else for now, click Save and now check our API name:

If we want to create a Vehicle object in Apex, we should do it like this:

Vehicle__c myVehicle = new Vehicle__c();

So, to clarify:

- a custom object with a label Vehicle has an API name of Vehicle__c;

- a custom field with a label Active has an API name of Active_c;

- we haven't talked about relationships so far, but just remember that a custom relationship between object has a "__r" sufix ( as a Relationship );

It's good time to mention that we also have an sObject generic type, which is the father of custom and standard objects. If, for example, we have a method which uses an sObject, but we don't know the type of that object ( an Account or a Vehicle__c ), we can use the generic type, like this:

sObject myGenericAccount = new Account(Name = 'My Test Account');
sObject myGenericVehicle = new Vehicle__c(Name = 'My personal car');

Check how we can add field data inside the brackets as well, without using dot notation. I don't like that approach, because I always want to see my variable name and the data I am setting to each field, but you can use it if you like.

Now, we have 2 generic sObjects. The difference is that an Account variable can reference only records of that object type, while the generic type can reference any sObject. You will get an error if you try something like this:

 Account errorAccount = new Vehicle__c();

In order to cast a generic sObject to a specific one and use dot notation on it's fields, you can do it like this:

Account myAccount = (Account)myGenericAccount();
myAccount.Name = 'Some test name';

There is a lot more to be told, but this is a quick overview of the sObject type. Keep in mind that only creating the sObject in Apex will not save it as a record in the data base, for that you will need DML ( Data Manipulation Language ) and also to retrieve data from the data base, you will need SOQL ( Salesforce Object Query Language ), which will be discussed later on.


Additional information:


Working with sObjects


SObject Methods


Object Basics

 
 
 

Comments


Proudly created with Wix.com

bottom of page