Sub topic 15
Using roll-up values inside formulas
This chapter assumes you can create a formula field and a roll-up summary field, and that you know how to guard against blank values in a formula. A roll-up summary field gives you a total on the master record. A formula field can then use that total to produce a ratio, a flag, or a label. This pairing is the usual way to get a value that a roll-up cannot calculate by itself, such as an average.
Why combine the two
A roll-up summary field supports only COUNT, SUM, MIN, and MAX. It has no average. You can build one by creating a SUM roll-up and a COUNT roll-up on the master object, then dividing them in a formula field on the same object.
A formula can read a roll-up field the same way it reads any other number field on the record. The formula is recalculated each time the record is read, so it always uses the current roll-up value.
Example: average line item amount
Suppose Invoice__c has two roll-up summary fields over Line_Item__c:
Total_Amount__c: SUM ofAmount__cLine_Item_Count__c: COUNT of line items
Add a Number formula field on Invoice__c named Average_Line_Amount__c:
IF(Line_Item_Count__c = 0, 0, Total_Amount__c / Line_Item_Count__c)The IF check matters. An invoice with no line items has a count of 0, and dividing by 0 causes a formula error. The check returns 0 instead.
Rules to remember
- The formula must live on the same object as the roll-up fields, which is the master object. It cannot read a roll-up directly from a child record.
- A roll-up summary field cannot reference a formula field on the child as its summarized field if that formula uses certain restricted functions, such as cross-object references to other objects. Check the field list in the roll-up wizard to see what is allowed.
- The formula field is a calculated value and is not stored like a regular number. You cannot set it in code, and you cannot use it as the field to total in another roll-up if the formula depends on cross-object references.
- If the roll-up is filtered, the formula sees only the filtered result. A SUM that counts only billable items gives a different average than a SUM of all items. Make sure the SUM and the COUNT use the same filter, or the ratio will mix two different sets of records.
Handling blank roll-ups
A roll-up on a master record with no children returns 0 for COUNT and SUM. MIN and MAX return a blank value. If your formula uses MIN or MAX, wrap them in BLANKVALUE so the result is not treated as empty:
BLANKVALUE(Earliest_Due_Amount__c, 0)Reading the value in Apex
Because a roll-up changes when child records change, query the master record after the change to see the new values, including formula fields built on them.
Exercise
An invoice has a SUM roll-up named TotalAmountc filtered to billable line items, and a COUNT roll-up named LineItemCount_c with no filter. A formula divides the first by the second. Why can the result be misleading, and how do you fix it?
Show answer
The SUM covers only billable items, while the COUNT covers all items, so the division mixes two different sets of records and gives an average that is too low. Fix it by adding the same filter, IsBillable_c EQUALS True, to the COUNT roll-up. Keep the zero check in the formula so an invoice with no billable items returns 0 instead of an error.