How to Create a rollup summary trigger for lookup relationship.
Before we Startup with the Trigger let’s check out with admin part that has to be done.
- First Create 2 Objects “Class” later “Student”
- Create a Lookup Relationship field on “Student” for “Class” with the name “Class”.
- Create a Number field on “Class” with the name “Number of Students”.
Let’s also learn something needed which is on which object we should write the Trigger. So as we know whenever the Student gets inserted or updated or deleted or undeleted we need to update Class, that is whenever there are Changes in Student then we should do changes in Class. So initial changes is taking place in Student that is the reason we need to write the Trigger on Student.
Now we are ready with the admin part to let see when we should call this trigger, so as per the question we should get the number of students related to a particular class on the Class Object field. So according to it when will the Number of Students change in Class Object, it is
- When we Create a new Student
- When a Student is updated and only if the Class field changes
- When a Student is deleted
- When a Student is undeleted
So by this, we come to know we should call this Trigger during “insert, update, delete and undelete”. Now the next thing that we should focus on is whether we should use “after” or “before” Trigger. Hereafter the Student is getting inserted only we come to know which class he belongs to and then later we will increment the Number of Students for that particular Class which the Student belongs. The same goes with the update, delete and undelete also, this is the reason that we have to use “after” Trigger
Now let’s get into the coding, there are some things we have to check those are,
- During Insert and Undelete(i.e. we are again adding or inserting back the deleted data) we should check whether the newly inserted data(i.e. Trigger.new) is not null and loop all the data and check if the Class field is not null, if it is not null then we can add that Class Id to a list for later use.
- During Delete, we are deleting the existing data(i.e. Trigger.old) is not null and loop all the data and check if the Class field is not null, if it is not null then we can add that Class Id to a list for later use.
- But when it comes to Update we should just check if the Class field is changing, so we will compare old and new data of the Class field if that is not equal that means the Students class has changed and we should decrement 1 student in the old Class and increment 1 in new Class, so we will check if the old and new Class fields or not null, then add those Class Id to the list.
Now that we have collected the Id of Class in the Set<Id>, we would get the count of Students associated to particular Class using the SOQL query ” Select count(Id), Class__c from Student__c where group by Class__c”. Now we use this query to get the Class Id and it corresponding count of students associated to it for only the Class Id’s which are present in Set<Id> and adding those values to a Map<String, Double> where String contains Class Id and the Double contains the Number of count of Students associated to it. Now get all the records of the Class which is present in Set<Id> into a List<Class__c> and check if that particular Class Id is present in the Map<String, Double>, if present then we are getting it value and placing it in the Number_of_Students__c field if not we are passing null value to the Number_of_Students__c field. Later we are checking if the List<Class__C> not empty then we are updating the list.