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
Kelly Kanches 6Kelly Kanches 6 

Need help with a trigger please!

Creatinga Trigger on the Opportunity updating the Description field of the Opportunity to 'Assessment Completed!!' after an Opportunity has been marked Won, and setting the Description to 'Opportunity Lost'
Best Answer chosen by Kelly Kanches 6
Abhishek BansalAbhishek Bansal
Hi Kelly,

Your requirement is not clear but still i have written a trigger for you by figuring out something that you want to achieve.
Below is the code of your trigger :
 
trigger updateDescription on Opportunity(before insert, before update){
	for(Opportunity opp : trigger.new){
		if(opp.StageName == 'Closed Won'){
			opp.Description = 'Assessment Completed!!';
		}
		else {
			opp.Description = 'Opportunity Lost';
		}
	}
}
Below steps explains the working of above trigger :
  1. If Stage of Opportunity is Closed Won than Description is Assessment Completed!!
  2. If Stage is other than Closed Won than Description is Opportunity Lost
Please let me know if your requirement is other than this so that i can modify the trigger code.

Thanks,
Abhishek
 

All Answers

Vishal Negandhi 16Vishal Negandhi 16
I will try and explain you how to write a trigger for the mentioned scenario. Please read through the notes between the code. 
Hope it helps you. 
 
// 1 : trigger on Opportunity to handle inserts and updates
trigger tgrOpportunity on Opportunity(before insert, before update){
	// 2 : iterating through records inserted/updated
	for(Opportunity opp : trigger.new){
		// 3: if it is a new opportunity, we set the description directly based on the stage
		if(trigger.isInsert){
			// for won opportunities
			if(opp.StageName == 'Closed Won'){
				opp.Description = 'Assessment Completed!!';
			}
			// for lost opportunities
			else if(opp.StageName == 'Closed Lost'){
				opp.Description = 'Opportunity Lost';
			}
		}
		// 4 : if it is an update, we should check if stage field has been changed. For any other field updates, trigger need not fire
		else if(trigger.isUpdate){
			// for won opportunties
			if(opp.StageName == 'Closed Won' && trigger.oldMap.get(opp.Id).StageName != opp.StageName){
				opp.Description = 'Assessment Completed!!';
			}
			// for lost opportunities
			else if(opp.StageName == 'Closed Lost' && trigger.oldMap.get(opp.Id).StageName != opp.StageName){
				opp.Description = 'Opportunity Lost';
			}
		}
	}
​}


Do let me know if you come across any issues or you have any questions around this. Happy learning!


 
Abhishek BansalAbhishek Bansal
Hi Kelly,

Your requirement is not clear but still i have written a trigger for you by figuring out something that you want to achieve.
Below is the code of your trigger :
 
trigger updateDescription on Opportunity(before insert, before update){
	for(Opportunity opp : trigger.new){
		if(opp.StageName == 'Closed Won'){
			opp.Description = 'Assessment Completed!!';
		}
		else {
			opp.Description = 'Opportunity Lost';
		}
	}
}
Below steps explains the working of above trigger :
  1. If Stage of Opportunity is Closed Won than Description is Assessment Completed!!
  2. If Stage is other than Closed Won than Description is Opportunity Lost
Please let me know if your requirement is other than this so that i can modify the trigger code.

Thanks,
Abhishek
 
This was selected as the best answer