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
AlecSAlecS 

Opportunity Trigger to Check Old Values

I have an Opportunity trigger. The trigger checks for the old value of the opportunity, specially the StageName=ClosedWon.

 

However, if I click on "New" opportunity and ClosedWon the opportunity the first time itself, the checking fails, because there's no old value at all for such opportunity.

 

How do I catch and fix this?

 

Thanks!

Vinit_KumarVinit_Kumar

Alec,

 

I think your Trigger is firing on both insert and update event.As insert event does not have Trigger.old values,it is throwing you an error.

 

Please change it to run only on update events.Change it from :-

 

trigger <TriggerName> on Opportunity(before insert,before update){

 

// your code

 

}

 

to

 

trigger <TriggerName> on Opportunity(before update){

 

// your code

 

}

vishal@forcevishal@force

Salesforce provides you two trigger variables.

 

Trigger.isInsert and Trigger.isUpdate

 

You can differentiate when to check for the old value using them.

 

Example:

 

if(Trigger.isInsert){

     // your logic

}

 

else if(Trigger.isUpdate){

    // check for the condition first and then execute your logic.

}