Dot notation in Salesforce: Accessing SObject Fields

Dot notation Introduction

A dot notation in Salesforce means connecting tow objects with the help of a dot. One interesting thing about the dot notation is, we do not just use it in Salesforce’s development but also in the declarative part. That means it is in every part of the Salesforce and a very important topic in Salesforce. It is mostly used in accessing the fields and even relationships from the objects. In this article we will learn more about it.

Dot notation in relationship

In Salesforce we have two types of relationships, Master-detail and Lookup relationships. Both can be traversed from child to parent and parent to child. In SOQL we use dot notation for a child to parent traversing and parenthesis for a parent to child relationship.

●       child-to-parent relationship

In child to parent relationship, we use dot notation to traverse the fields in a parent object from the child object. But one important to remember is for custom objects and fields we append “__c” to the names. But while traversing through the fields of a  parent object we have to use append “__r” to traverse. Let’s an example to get this better.

Example:

  • SELECT Lastname__c, Leave_type__c, No_of_Days__c  FROM  leave__c

     

Explanation:

Here, the Student object is the parent object and Leave is the child object. We are trying to retrieve the fields from the Leave object along with the fields from its parent object as we used the dot notation here to query.

●       Parent-to-child relationship

In parent to child relationships we simple use parenthesis and write another query to retrieve the child objects by writing a new query inside it again for the child fields. Let’s see an example for this to understand this better.

Example:

  • SELECT Lastname__c, Email__c ( SELECT Leave_type__c, No_of_Days__c FROM Leave) FROM Student__c

     

Explanation:

Here, as you observe to retrieve the Leave object fields we wrote another query inside parenthesis as they are the child fields of the Student object.

Accessing SObject Fields

In apex, we can create and access sObjects and their fields. In order to access the sObjects fields, we use dot notations which simply means to access the fields of the sObjects. After that we can create, edit, update or delete the fields. But what we have to remember is we cannot modify some system-generated fields like created by fields by we can only access those fields. Let’s see an example to understand this better.

 

Examples

This is an example how to create the sObjects and fields in salesforce through apex.

public class CreatingObjects {
   
    public static void creatingObjectsAndField(){
        Account acc = new Account();
        acc.Name = 'Harry';
        acc.Phone = '9876543210';
        insert acc;
    }
   
    public static void retrivingRecords(){
        List<Account> accList = new List<Account>();
        accList[0].Name = 'Krish';
        accList[0].Phone = '9876543210';
       
        accList[1].Name = 'John';
        accList[1].phone = '9876543212';
        insert accList;
    }


}

 

 

This is an example how to access the sObjects and fields in salesforce through apex.

public class retrievingRecords {
   
    public static void accesingObjectsAndField(){
        Account acc = new Account();
        acc = [SELECT Name, Phone FROM Account LIMIT 1];
       
        List<Account> accList = new List<Account>();
        accList = [SELECT Name, Phone FROM Account LIMIT 5];
    }
}

 

 

Reference

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SObjects_accessing_fields.htm

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm

https://www.sfdc99.com/2013/10/05/dot-notation-navigating-relationships-and-using-methods/

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.