I have a context for QuoteLineItems pricing that uses the list price from the current Pricebook and applies a series of factors (defined in custom fields of both Quote and QuoteLineItems) to compose different pricing stages of the item. I have this scenario implemented via WFR + Field Updates, but trying to migrate this to triggers, since I need more control of execution order for later improvements. This is the scenario I'm trying with triggers:
- QuoteLineItem has a Formula field
Effective_Unit_Price__c
, which is a really long formula field; - QuoteLineItem has a Number Field
Effective_Unit_Price_2__c
which is updated viabefore update
trigger with contents fromEffective_Unit_Price__c
, so we can use this new one on other formulas ahead in this pricing stages I mentioned. I need this because the previous field was already too long; (this update was being done by field update) - QuoteLineItem has a Formula field
Total_Price__c
which is a formula field referencing thatEffective_Unit_Price_2__c
plus some Quote's field (cross-object reference). - QuoteLineItem has another Number field called
Total_Price_2__c
which is updated viabefore update
trigger:
... item.put('Total_Price_2__c', item.get('Total_Price__c')); ...
- Quote has a roll-up summary field which sums up this
Total_Price_2__c
(that is the reason I make that copy, because there is a restriction of roll-up for cross-object formula fields);
So, basically I want this propagation of values:
QLI.Effective_Unit_Price__c -> Qli.Effective_Unit_Price_2__c -> QLI.Total_Price__c -> QLI.Total_Price_2__c
The order of those before update
triggers in QuoteLineItem (it is a single trigger, using methods from a handler class):
trigger QuoteLineItemTrigger on QuoteLineItem (before insert, before update, after delete) {
if (trigger.isBefore && trigger.isUpdate) {
Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([... QUERY ...]);
List<QuoteLineItem> new_items = trigger.new;
// Updating some long-formula fields of QuoteLineItems
QuoteLineItemTriggerHandler.UpdateWkFields(new_items, entries);
// Copies calculated values from formula fields with cross-object formula to new fields, to be used in roll-up summary fields
QuoteLineItemTriggerHandler.CopyCrossObjectFormulaFields(new_items);
}
However, my desired output is not being achieved. Here is an example:
- Before running triggers,
Effective_Unit_Price__c
is 1000 and all other fields are 0; - First trigger runs (Effective Unit Price fields), populates
Effective_Unit_Price_2__c
with 1000; BothTotal_Price__c
andTotal_Price_2__c
are still 0; - Second trigger runs (Total_Price fields) but since the source value is still 0, target field remains as 0;
What I am doing wrong? Is there some other better solution to get this desired propagation of values? Or shouldn't I be moving to triggers? Any advice is very appreciated.