I have created a custom Object called
Application__c that has a lookup relationship to the
Opportunity object. I have a field called
total_applications__c on the
Opportunity which aims to represent the total number of applications related to that opportunity.
I trying to populate that field though an apex trigger on the
Application__c object that operates on
after insert, after delete and after undelete events.
Considering the insert event :
My initial approach is that each time an application is being inserted I
-> create an oppset with all the opportunities that are being linked to from the applications that have been triggered
-> loop through all the applications that are link to an opportunity from the oppset
-> add the number of applications related to each opportunity in a map
-> loop though all the related opportunities from the applications that have currently being inserted
-> update that total_applications__c field with the computed values from the map.
However I feel that I can achieve the same functionality in fewer steps. Is there a better way of achieving what I described above?