salesforce sObject || Generic sObject

Technical Pustak
1

What is sObject in Salesforce 

    

 

    In Salesforce, an sObject (short for "Salesforce Object") is a data structure that represents a real-world object, such as a customer or a product.

sObjects are used to store and manage data in Salesforce, and they are the building blocks of the Salesforce data model. Each sObject has a set of fields that define the data that can be stored in it, and it can be related to other sObjects to create complex data structures.

Some examples of standard sObjects in Salesforce include Account, Contact, Lead, Opportunity, and Case. Salesforce also allows users to create custom sObjects to fit their specific business needs.


Use of sObjects

    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 an sObject in Apex. For example, the Rahul account record corresponds to an Account sObject in Apex. The fields of the Rahul record that you can view and modify in the user interface can be read and modified directly on the sObject as well. 

The following table lists some populated fields of the Rahul account example record. 



Account Field

Value

Id

0012w00001W2FKhAAN

Name

Rahul

Phone

1213458594

website

https://technicalpustak.blogspot.com


Standard and custom object records in Salesforce map to their sObject types 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 Student corresponds to the Student__c sObject in Apex.

 

Create Custom Object 

 To create a custom sObject in Salesforce, you need to follow these steps:

1. Go to Setup by clicking on the Gear icon in the top-right corner of the screen and selecting "Setup."

2. In the left-hand menu, under "Platform Tools," click on "Objects and Fields" and then select "Object Manager."

3. Click on the "Create" button to create a new custom object.



4. Choose the object's label and object name. The object name is used to reference the object in code and API integrations.





5. Here 

  • lebel            ->  Student
  • Plural label ->  Stduents
  • apiname      - > Student__c     

6. Save the custom object.

 

Once you've created a custom sObject, you can use it to store data and build custom functionality in Salesforce, such as triggers, workflows, and Visualforce pages. You can also use the Salesforce API to create, read, update, and delete records of your custom object.


Create sObject Variables


To create an sObject, you need to declare a variable and assign an sObject instance to it. 

 The following example creates an sObject of type Account with the name Rahul and assigns it to the acct variable.


           Account acct = new Account(Name='Rahul');



sObject and Field Names


    Apex standard or custom sObjects and their fields using their unique API names.  

API names of object and fields can differ from their labels. For example, the Employees field has a label of Employees and appears on the account record page as Employees but its API name is NumberOfEmployees. To access this field in Apex, you’ll need to use the API name for the field: NumberOfEmployees.

 

 

Rules for using API names for custom objects and custom fields.

 For custom objects and custom fields, the API name always ends with the __c suffix. For custom relationship fields, the API name ends with the __r suffix. 

For Example:- 

  • A custom object with a label of Student has an API name of  Student__c.
  • A custom field with a label of Description has an API name of  Description__c.
  • A custom relationship field with a label of Items has an API name of Items__r.

 In addition, spaces in labels are replaced with underscores in API names. For example, a custom field name of Employee Seniority has an API name of Employee_Seniority__c.

 

How to find api name

     For both standard and custom objects, look up the object and field API names in your org. From Setup, click the Object Manager tab to the right of the Home tab, and then click your object’s name.

 

 

Create sObjects and Adding Fields

 

Before you can insert a Salesforce record, you must create it in memory first as an sObject. Like with any other object, sObjects are created with the new operator:

 The API object name becomes the data type of the sObject variable in Apex. In this example, Account is the data type of the acct variable

 

 

There is two ways to add fields:-

 1. The fastest way to add fields is to specify them as name-value pairsinside the constructor:-

     Account acct = new Account(Name='Rahul', Phone='(415)555-1212', NumberOfEmployees=100);

 2. Alternatively, you can use the dot notation to add fields to an sObject.         The following is equivalent to the previous example, although it takes a      few more lines of code.


    Account acct = new Account();

    acct.Name = 'Rahul';

    acct.Phone = '(415)555-1212';

    acct.NumberOfEmployees = 100;

 

 

Generic sObject

     When you use the specific sObject data type, such as Account for a standard object or Book__c for a custom object called Book, when working with sObjects. However, when you don’t know the type of sObject your method is handling, you can use the generic sObject data type.

 Variables that are declared with the generic sObject data type can reference any Salesforce record, whether it is a standard or custom object record.

 Example of Generic sObject:-

                 sObject sobj1 = new Account(Name='Rahul'); 

                 sObject sobj2 = new Book__c(Name='Rich Dad Poor Dad');



Tags

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. Hey learners feel free to post your doubts and suggestions

    ReplyDelete
Post a Comment
Our website uses cookies to enhance your experience. Learn More
Accept !