+ Start a Discussion
NerijusNerijus 

trigger.old holds new value

Hi there,

I have a trigger on OpportunityLineItem and I need to compare formula field old and new values. That formula field gets value from another related object which is CPQ Quote Line and I need that trigger to fire on OpportunityLineItem when value changes on Quote Line.
When value changes on Quote Line and fires the trigger on OpportunityLineItem I get the same new value on Trigger.new and Trigger.old list
Anyone knows how to get old variable?
 
trigger OpportunityLineItemTrigger on OpportunityLineItem (after insert, before update, after update)
{
    
    if (Trigger.isBefore && Trigger.isUpdate)
    {
        System.debug('BEFORE');
        for (OpportunityLineItem oli : Trigger.new)
        {
            System.debug(oli.Quantity_Formula__c);
        }
        for (OpportunityLineItem oli : Trigger.old)
        {
            System.debug(oli.Quantity_Formula__c);
        }
    }
    
    if (Trigger.isAfter && Trigger.isUpdate)
    {
        System.debug('AFTER');
        for (OpportunityLineItem oli : Trigger.new)
        {
            System.debug(oli.Quantity_Formula__c);
        }
        for (OpportunityLineItem oli : Trigger.old)
        {
            System.debug(oli.Quantity_Formula__c);
        }
    }
}

 
Best Answer chosen by Nerijus
Adheesh GuptaAdheesh Gupta
Since you are using a formula field on OpportunityLineItem which gets its value from the field in parent object (CPQ Quote Line), so it will update its value as soon as any CPQ Quote Line record is modified. Hence using a trigger on detail object won't suffice.

To fulfill your requirement, i.e. compare old and new values of formula field in OpportunityLineItem trigger, you can follow below steps:

1.) Create a text field on OpportunityLineItem object, and populate it with the old value of the field of CPQ Quote Line (which was referenced by the formula field). You can do it via Trigger.old in BeforeUpdate of CPQ Quote Line trigger.
2.) Now you have the old value of that formula field in a separate text field, so you can simply compare it with the new value in OpportunityLineItem trigger.

Note: Since formula field updates its value automatically as soon as the parent field it is referencing to, modifies. So, to proceed with such scenarios, either you need to write code in parent object trigger to store the value somewhere (as suggested above) or you need to create any process builder or workflow rule to do this job for you, whenever the parent record field gets changed.

All Answers

Adheesh GuptaAdheesh Gupta
Since you are using a formula field on OpportunityLineItem which gets its value from the field in parent object (CPQ Quote Line), so it will update its value as soon as any CPQ Quote Line record is modified. Hence using a trigger on detail object won't suffice.

To fulfill your requirement, i.e. compare old and new values of formula field in OpportunityLineItem trigger, you can follow below steps:

1.) Create a text field on OpportunityLineItem object, and populate it with the old value of the field of CPQ Quote Line (which was referenced by the formula field). You can do it via Trigger.old in BeforeUpdate of CPQ Quote Line trigger.
2.) Now you have the old value of that formula field in a separate text field, so you can simply compare it with the new value in OpportunityLineItem trigger.

Note: Since formula field updates its value automatically as soon as the parent field it is referencing to, modifies. So, to proceed with such scenarios, either you need to write code in parent object trigger to store the value somewhere (as suggested above) or you need to create any process builder or workflow rule to do this job for you, whenever the parent record field gets changed.
This was selected as the best answer
NerijusNerijus
Hey Adheesh,

you say "it will update its value as soon as any CPQ Quote Line record is modified" isn't OpportunityLineItem trigger supposed to fire as soon as any CPQ Quote Line record is modified if there is a formula field that references field from Quote Line?

I understand what you saying but it doesn't make much sense for me. I would like to understand this issue and not just know a workaround. Maybe there is any documentiton to read on this topic?
Adheesh GuptaAdheesh Gupta
Hi Nerijus,

Using a formula field in detail object doesn't mean that if the master object record is updated, it will fire the trigger for master and then detail as well.

Instead what actually happens is updating the master record will only fire the trigger on master object (in your case-CPQ Quote Line) even if any field is being referred by any formula field of detail object , and the transaction gets over. So, when you use the trigger on detail object (OpportunityLineItem) to compare the old and new values of formula field, it fetches the updated value in both trigger.old and trigger.new, since it's been executed in a new transaction.

The workaround for it has already been given in previous answer.

Please mark this answer as solved if it answers your query.

Thanks,
Adheesh