[SalesForce] Update Opportunity Stage when DocuSign Envelope Status is ‘Signed’

I am very new to triggers but would like to set up a simple trigger. Any help is greatly appreciated! Pseudo code below.

on insert or update

if (Docusign Envelope Status is equal to 'Signed' AND Opportunity Status does not equal ‘Agreement Signed Pending Rollout’){

     update Opportunity Stage to ‘Agreement Signed Pending Rollout’

}  

Will this work?
How should I go about compiling the trigger?

variables                        API Name
Docusign Envelope Status         dsfs__Envelope_Status__c   
DocuSign Oppty                   dsfs__Opportunity__c

Best Answer

This simple trigger will do the trick.

trigger opportunityStage on dsfs__Opportunity__c(before insert, before update){
    for(dsfs__Opportunity__c o : trigger.new){
        if(o.dsfs__Envelope_Status__c == 'Signed' && o.stage != 'Agreement Signed Pending Rollout'){
            o.stage = 'Agreement Signed Pending Rollout'
        }           
    }
}

Explanation:

  1. Why used before triggers? When we use before triggers, the values are not yet saved to database and any changes we made in trigger will get saved to database without any extra DML.
  2. What is trigger.new? It is trigger context variable which holds the list of currently modified or inserted records.
  3. Why == in if condition but = in next line? == is comparison operator which is used to compare two values. = is assignment operator which is used to assign value in right hand side to left hand variable.

Hope it helps.

Related Topic