This trigger updates an Opportunity’s Description to indicate its status: “Opp is Closed Lost,” “Opp is Closed Won,” or “Opp is Open,” based on its Stage.
Learn more about triggers here: YouTube playlist.
Solution:
trigger OpportunityTrigger on Opportunity (before update) {
if(Trigger.isUpdate){ if(Trigger.isBefore){
OpportunityTriggerHandler.updateDesc(Trigger.New,
Trigger.oldMap);
}
}
}
public class OpportunityTriggerHandler { public static void
updateDesc(List oppList,Map oldMap){
for(Opportunity opp:oppList){
if((oldMap==null)||(opp.StageName!=oldMap.get(opp.Id).Stage
Name)){ if(opp.StageName==’Closed Won’){ opp.Description=’Opportunity is
Closed won’;
}else if(opp.StageName==’Closed Lost’){ opp.Description=’Opportunity is closed lost’;
}else{ opp.Description=’Opportunity is open’;
}
}
}
}
}