I have a roll up summary field on one of my objects. Because roll up summary fields do not support smart dates or formulas, I am forced to manually input a specific date range that I want to summarize child records from. Is it possible to update the roll up filter from apex? For example, if I scheduled apex to run at the start of each month, I want to change the roll up filter to only roll up records from the start of the current month. Is this possible?
1 Answer
No - or at least not unless you use something like apex-mdapi to talk to the Metadata API, which is possible, but which I would not consider the most expeditious solution to this objective.
I'd propose (at least) three options:
- Establish a custom checkbox field
This_Month__c
the child object. Write Scheduled Apex set to fire on the last day of the month (or the first of the next month) to query for all objects withThis_Month__c
set and clear it. Use a Trigger or a Process Builder or Workflow Rule to setThis_Month__c
totrue
whenever a new object is created whose date (if other than the created date) is in the current month. Then, build your Roll-up Summary Field usingThis_Month__c
as your filter rather than a date range. - Use the same field and Process/Trigger as above, but instead of Scheduled Apex, do some malarky with scheduled actions. Add a new Date formula field on the child object
Last_Date_Valid__c
, whose value is the last day of the month of that object's Date field (or Created Date, if you're using that). (Formula expression left as an exercise, but feel free to ask if you'd like help constructing it). Then, use a Process Builder or Workflow Rule Time-Dependent Action to unsetThis_Month__c
zero days afterLast_Date_Valid__c
. Again, build your Roll-up Summary Field using theThis_Month__c
field as your criterion. - Install Declarative Lookup Rollup Summaries, build a rollup using the criteria you need (not a native Roll-up Summary Field) and run it in Scheduled mode, refreshing on the first of the month.
I'd recommend just going with DLRS because it will save you almost all of the implementation and maintenance.
-
1and the dlrs rollup criteria is maintained in custom metadata (__mdt)– cropredyCommented Sep 5, 2018 at 22:46
-
Shucks, I'm dealing with a large amount of child records, so all three of those solutions will have it's problems. Perhaps I can monkey with DLRS enough to make it work for me though, thanks! Commented Sep 6, 2018 at 14:20
-