Thursday, April 18, 2013

Field update using Trigger based on RollUp Summary field of an object in Salesforce


Last week I faced a situation. I had to update a field in an Object from Trigger based on a Roll up Summary field latest value in that Object.

Let Object name is Inventory. Roll Up Summary field name is rollUpInventory__c.

And in Trigger, I had to update another field testInventory__c based on previous field.

Now while trigger is fired, I experienced that every time its never getting latest Roll Up summary field value. Then I searched and found that Salesforce had steps in its Save logic. When trigger is fired, then Roll Up summary field is at all updated. Here is steps of Database Save logic in Salesforce.


1- Old record loaded from database (or initialized for new inserts)
2- New record values overwrite old values
3- System Validation Rules
4- All Apex “before” triggers 
5- Custom Validation Rules
6- Record saved to database (but not committed)
7- Record reloaded from database
8- All Apex “after” triggers 
9- Assignment rules
10- Auto-response rules
11- Workflow rules
12- Escalation rules
13- Parent Rollup Summary Formula value updated (if present)
14- Database commit
15- Post-commit logic (sending email)

1 comment: