A roll-up trigger in Salesforce is a type of trigger that allows you to summarize data from related records and store that summary data on a parent record.
Roll-up triggers are useful in situations where you need to maintain summary information on a parent record, such as the total number of child records, the sum or average of a field on the child records, or the maximum or minimum value of a field on the child records.
You can achieve some types of data summarization using Roll-Up Summary Fields in Salesforce. However, Roll-Up Summary Fields have some limitations. They can only be used on Master-Detail relationships or Lookup relationships with the "Roll-up Summary" option enabled. They are also limited to performing calculations on a single field, and only on immediate child records. If you need to perform more complex calculations or work with related records beyond the immediate child records, you may need to use a Roll-Up Trigger.
Now let understand it using an simple example: Let’s say we want to create a roll-up summary on the Account object to display the total amount of all Closed-Won Opportunities associated with that Account.
To accomplish this, you would need to create a trigger on the Opportunity object that fires when an Opportunity is created, edited, or deleted. The trigger would then update a custom field on the related Account object with the total amount of all Closed-Won Opportunities.
First, we have to create a currency field on the Account object and set it read only.
This trigger will calculate the sum of all Closed-Won Opportunities for each related Account, and then update the Total_Closed_Won_Amount field on the Account with the calculated value.
Get in touch with our expert Salesforce consultants to streamline your business processes and maximize efficiency.