Relationships in Salesforce
Relationships in Salesforce :-In our previous Salesforce Developer Tutorial we have learned about Inner Join and Outer Join relationship in Salesforce. In this Salesforce Developer Training Tutorial we are going to learn about Semi Join and Anti Join relationships in Salesforce.
How Semi Join and Anti Join Relationships in Salesforce are Helpful?
Semi Join and Anti Join are used to to enhance the functionality of parent-to-child queries and child-to-parent queries in Salesforce Object Query Language and also they they are useful in create child-to-child relationships.
These Semi Join and Anti Join allows records to filter from one object to another object using sub query.
Parent to Child relationships in Salesforce Example
- Select Account.name, (Select contact.firstname, contact.lastname FROM Account.Contacts )FROM Account.
In Parent-to-Child Relationships name will be plural of the child object and it contains sub query.
SOQL Semi Join example.
- Select Id, Name From Account WHERE Id IN (Select Account__c FROM Proj__c WHERE status__c = ‘Yellow’).
We have to read above SOQL query from the bottom to understand. Sub query identifies all projects which are in yellow status and also it returns their unique Id and Name.
SOQL Anti Join Example
- Select Id, Name FROM Account WHERE Id NOT IN (Select Account__c FROM Proj__c WHERE status__c = ‘Green’).
Anti Join is Purely reverse to Semi Join. In Anti Join we use Key words like NOT, IN in sub queries to exclude certain records from the parent object. From above SOQL Query we are trying to retrieve all records from project object except the records which are in Yellow status and also we excluding the records which are not associated with parent object.