function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tobibeertobibeer 

Trigger & Roll-Up Summary Field -> force update / recalculate

I am struggling with writing some (after event) trigger wherein it seems that a roll-up summary field that I want to check is not yet recalculated. Doing a database lookup for the children of a given master node reveals that the database indeed reflects any changed records, yet the corresponding roll-up does not.

 

Thus, is there a way to force-update a roll-up summary field, hoping that this will recalc to reflect the changes?

 

I mean, I am using a simple count of child objects and I guess I could easily calculate that in my trigger ...but that's exactly what I intended to use the roll-up summary field for, to simplify coding and to use it in other formula fields.

 

Cheers, Tobias.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Rollup summary fields are calculated in step 13 of the database save logic, while triggers execute during step 4 (before event triggers) and step 8 (after event triggers). Please check the documentation under KB Article 104371, "In what order are automation rules and Apex triggers evaluated?". In the event of recursive triggers, the recursive triggers only execute only up through escalation rules (step 12), then return control to the calling trigger. This means that all rollup summaries will retain their prior values until there is no possibility of Apex triggers firing and the system can continue with the rollup, commit, and post-commit steps. If you need these values for some sort of calculation, you must use a @future method instead, which guarantees that the entire batch of records have had their summary values updated, or you must query the database for the values you're looking for.

All Answers

Ritesh AswaneyRitesh Aswaney

This shouldn't usually happen unless you've only just created this Rollup Summary field and there are a large number of records.

 

If this is the case, there is an initial lead time, where the Rollup Summary Field needs to calculate and is available for access.

sfdcfoxsfdcfox

Rollup summary fields are calculated in step 13 of the database save logic, while triggers execute during step 4 (before event triggers) and step 8 (after event triggers). Please check the documentation under KB Article 104371, "In what order are automation rules and Apex triggers evaluated?". In the event of recursive triggers, the recursive triggers only execute only up through escalation rules (step 12), then return control to the calling trigger. This means that all rollup summaries will retain their prior values until there is no possibility of Apex triggers firing and the system can continue with the rollup, commit, and post-commit steps. If you need these values for some sort of calculation, you must use a @future method instead, which guarantees that the entire batch of records have had their summary values updated, or you must query the database for the values you're looking for.

This was selected as the best answer
tobibeertobibeer

So it is done... querying the database ...which is actually not too tough to accomplish.

 

@sfdcfox, would you mind pointing to the place where those KB articles are found? ...I just recently entered the apex development world.

 

Thanks, Tobias.

sfdcfoxsfdcfox

Login to Salesforce, and click on Help (Developer) or Help & Training (Production) link in the upper-right corner. There's a wealth of information in this window, if you know what to look for. I happen to know this because it crops up from time to time. I admit, this sort of information is somewhat esoteric, in that most people never attempt what you're trying to do. I don't think that Developer training covers the fringe cases. This is what makes our community so valuable; we all have experienced various parts of the platform and have experienced some of these fringe cases from time to time. The documentation is there, but there are literally thousands of pages of information, so finding the exact root cause of something can be troublesome.