1

I'm writing an apex trigger in order to do stuff when a custom field is modified in an opportunity.

trigger propalToDrive on Opportunity (after update) { 
    Opportunity oldOpp = Trigger.oldMap.get(Opportunity.Id);
    if (Opportunity.get(Lien_propale__c) != oldOpp.get(Lien_propale__c)) 
    {
        //Do things      
    }
}

The developer console throws me an error : line 3 : Variable does not exist: Lien_propale__c But it does ... How am I supposed to do to ?

  • I think "Opportunity.get(Lien_propale__c)" is not the right way to get the current value. Check out some examples. Also, your version here won't handle multiple opportunities (which can happen). – Jagular Oct 3 '14 at 16:30
2

A trigger operates on a trigger set, which is a list of up 200 records per chunk, opportunities in this case. Trigger.New contains all of the opportunities in the list with the values after the update, insert or undelete. Trigger.Old contains all of the opportunities with the values before the update or delete. In order to operate on an opportunity in a trigger set, you must either loop through the entire set or reference a single opportunity by its index in the collection. It's worth pointing out that referencing the trigger set by index (e.g. Trigger.New[0]) means that your trigger is not bulk safe. Your trigger would fail to process any update DMLs made to a list of opportunities unless it was made bulk safe.

A common trigger pattern is starting your trigger with a for loop to collect your records for processing. Here is an example of your code that is bulk safe:

trigger propalToDrive on Opportunity (after update) { 
    for (Opportunity newOpp : Trigger.New) {
        Opportunity oldOpp = Trigger.oldMap.get(newOpp.Id);
        if (newOpp.Lien_propale__c != oldOpp.Lien_propale__c) 
        {
           //Do things      
        }
    }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.