As we all know we can use roll-up summary field logic only for the master-detail relationship, but in case you have the business requirement to implement the same logic for lookup relationship then you have to think of an alternative way to cater to the requirement, there can be multiple ways to implement but in this blog, we will use a trigger to implement roll-up summary field logic. So before wasting any time, let's get started.😊 Read till the end to know the bonus tip.
This is the salesforce CRM blog where you will learn about basics of salesforce, Triggers,Apex , integration, and lightning components: LWC and also interview questions with scenarios that will guide you to crack salesforce developer interviews.
Wednesday, 23 December 2020
Episode 13 : RollUp Summary Trigger for Lookup Relationship
Monday, 21 December 2020
Episode 12:Relationship Queries in Apex -Salesforce
In this blog, we will deep dive into the concept of relationship queries. So Before wasting any time, let's let started.😊
Most of the time we need to query the data from more than one standard or custom object at a time i.e we need to traverse either from parent to child or from child to parent objects. SOQL (Salesforce Object Query Language) provides us the syntax to write these queries which are called the Relationship Queries. We can visualize the Relationship Queries same as Joins in SQL.
Note: If you are using multiple objects in the same query to fetch the data, always remember those objects must be related to each other i.e there must be a parent -to- child or child-to-parent relationships between those objects.
Parent to Child Relationship Query
We use this query when we are referring to child object records from the parent object. Here, the main object for the query is the parent object.
Standard Object Relationship
Let's take an example of the Account and Contact object where Account is the parent of Contact Object.
In order to traverse parent-to- child or child-to-parent relationship, relationship name is given to each relationship.
Drill down on the Account Name and below screen will appears and here you can find the child relationship name which is contacts in this case.
Custom Objects Relationship
For Example, there are two custom objects one is Company__c and another one is Employee__c.
One Company can have multiple employees so they share one -to -many lookup relationships where the company is the parent object and the employee is the child object.
Now If you want to reach the employee object record from the company object, you have to write your query on the parent object (Company__c in this case)and fetch the related child records( employees).
Before writing this query we need the relationship name between those objects. Here the relationship name is employees__r.
Select Id,Name ,(select Id,Name,Company__c from Employees__r) from Company__c
Note: When you are travesing from parent to child in Custom Objects Relationship, then use
plural of the child object name( without the __c) and append an __r .
NOTE: Only 1 Level of the nested query is allowed( you can remember as 1 LEVEL DOWN)
Child to Parent Relationship Query
Before jumping directly to query we need to know something.Yes , you are right we need the relationship Name between two standard objects.
Lets again take the example of Account and Contact Object where Account is the parent of the Contact Object and the value of relationship name in contact is Account and you can traverse the relationship by specifying the parent using dot notation.
For Example,
SELECT Id, Account.Name, Account.Industry, Account.Rating FROM Contact limit 10
The above query will give below results:
Custom Objects Relationship
When you use a child-to-parent relationship, you can use dot notation with the relationship name as given below:
select id, name,company__c,company__r.name from employee__c limit 10
NOTE: You can Access up to 5 levels of parents using DOT Notation( i.e 5 LEVEL UP)
Thanks for reading my blog.Stay tuned till then keep learning and keep sharing.
Episode 13 : RollUp Summary Trigger for Lookup Relationship
As we all know we can use roll-up summary field logic only for the master-detail relationship, but in case you have the business requiremen...
-
In this blog, we will deep dive into the concept of relationship queries. So Before wasting any time, let's let started.😊 Most of the t...
-
As we all know we can use roll-up summary field logic only for the master-detail relationship, but in case you have the business requiremen...
-
In this Blog, we will have insights about Apex Triggers and how to apply trigger concepts in real-time scenarios. This Blog is for the Begin...