Creating sObject in salesforce

 sObject in Salesforce is an object that can be stored in the platform database. Salesforce provides sObject (Salesforce Object) data type for standard or custom objects in Salesforce. All the Salesforce standard and custom objects are known as sObjects. We can directly use them in Apex, Because Apex is tightly integrated with the database, you can access Salesforce records and their fields directly from Apex. Every record in Salesforce is natively represented as a sObject in Apex. 

Here are some common sObject type names in Apex used for standard objects. 

  • Account
  • Contact
  • Lead
  • Opportunity

If you’ve added custom objects in your organization, use the API names of the custom objects in Apex. For example, a custom object called Salary corresponds to the Salary__c sObject in Apex.  In Apex, every sObject will be used by its API name.

Creating sObject in Salesforce

In Salesforce, we have certain standard and custom objects and fields available to us. In order for us to work with records within Apex Salesforce provide the sObject (Salesforce Object) data type. This is the base data type for any standard or custom object in Salesforce, and from this base type, we get specific instances of object types for our custom and standard objects.

What does this all look like in practice? Let’s say we have 2 objects we want to work with, the standard account object and a custom job object. The API or developer name for the account object is Account and for our custom job object is Job__c. To declare variables of this type we then simply use the API name in our definition: 

Account acc = new Account();
acc.Name = 'Prasanth';
 Acc.Site = '';
Acc.Phone = '0000000012';
Insert acc;

Firstly, we have the new Account data type being applied to the variable we are defining called acc on the top line, which we have seen before. However, after the = we have newAccount() – what does this mean? Unlike our primitive data types, an Account represents something more complex, an individual account record with our fields on it such as Name. When we are working with account records, and in this instance creating a new account record to work with, we have to tell Salesforce this is a new account record instance. We do this using the keyword new, and adding the parentheses after the word Account. The statement newAccount() tells Apex we want a new blank Account record instance for us to work with. 

Because our acc variable has the Account sObject data type we can also access the fields on the account as we do on line 2 with the dot notation – acc.Name gets the Name field (or property) of the acc variable, and we assign the value Prasanth.

We have successfully created an Account using Salesforce Apex.

Scroll to Top