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 List we get the record of Class and its related Students through query, then check if the List of Class id empty or not, if not then assign the size of its related Students to the “Number of Students” field. and that record to separate List for a later update. Now we check if the List of Class that has to be updated is empty or not, if not we update all those Class records.