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
baseball123baseball123 

Can't get trigger.old[0] on before update

Is it possible to get trigger.old[0].value on BEFORE UPDATE? I want to compare the old value and new value in order to decide to go to run the rest of the trigger in order to reduce number of soql queries.

 

Thanks in advance,

sfdcfoxsfdcfox

Yes, trigger.old and trigger.new are both available in "before update" triggers. Your problem most likely lies with some other problem with your code. You might post it here and see if somebody can help you with your issue.

vishal@forcevishal@force

Yes, you have access to it as said by sfdcfox.

 

Since you want to compare values, you can even use trigger.oldMap.

 

Example:

 

I want to process only those Accounts in my trigger where Account.Name has changed.

 

This snippet should help you:

 

for(Account acc : trigger.new) // this iterates through all the accounts

{

     if(trigger.oldMap.get(acc.Id).Name != acc.Name) // comparing the current Account.Name with it's previous value using oldMap

     {

          setTemp.add(acc.Id); // add the Account Id if Name has changed.

     }

}

 

now I can process those Accounts as per my requirement.

 

Hope this helps!

baseball123baseball123

Thanks for the prompt response. The logic is if Oppty.Discount__c value changed, I don't want to run the rest of the trigger. But in my debug, trigger.new and trigger.old both give me the new value. I'm not able to get the old value. Please advise.

 

trigger OpportunityUpdate on Opportunity (before insert, before update) {

system.debug(Trigger.new[0].Discount__c is ' + Trigger.new[0].Discount__c + ' -------- trigger.old[0].Discount__c ' + trigger.old[0].Discount__c);

 

if (Trigger.new[0].Discount__c  == trigger.old[0].Discount__c)

{

 

//rest of the trigger

}

}

sfdcfoxsfdcfox

To work with just the opportunities that have not changed, you probably want this:

 

map<id,opportunity> opps = new map<id,opportunity>();
for(opportunity o:trigger.new) {
 if(o.discount__c == trigger.oldmap.get(o.id).discount__c) {
  opps.put(o.id,o);
 }
}
for(opportunity o:opps.values()) {
  // These discount__c values have not changed.
}

You always need to make sure that triggers are bulkified. If this is a rollup field, note that rollups do not calculate until the end of a transaction, so your trigger will never see the new value, only the old value. You'll have to explicitly query for the new value, which should be available your trigger, since the line items will be committed for the transaction before your opportunity trigger is called.

 

Edit: To clarify, you'll need to determine how your Discount__c is being rolled up (e.g. SUM(OpportunityLineItem.Discount__c)), then do this:

 

for(AggregateResult ar:[SELECT OpportunityId Id, SUM(Discount__c) Discount FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.new GROUP BY OpportunityId]) {
  if(Trigger.newMap.get((Id)ar.get('Id')).Discount__c == (Decimal)ar.get('Discount')) {
    opps.put((Id)ar.get('Id'),Trigger.newMap.get((Id)ar.get('Id')));
}

 

vishal@forcevishal@force

Well, to clarify, in first place, this comparison should be done only in case of "before update". In a "before insert", you'll never have any old values. I don't see you making any condition checks whether the trigger is for an insert case or update case.

 

Try this:

 

trigger OpportunityUpdate on Opportunity (before insert, before update) {
 

if (Trigger.isInsert || (Trigger.isUpdate && Trigger.new[0].Discount__c  == trigger.old[0].Discount__c))

{

 //let me clear, this checks only for the first record. Won't work for multiple records (bulk). For that, you'll need to //use the approach mentioned above (iterating through the trigger.new and checking with trigger.oldMap)

//rest of the trigger

}

}

 

Hope this helps!