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
Rahul Singh 1384Rahul Singh 1384 

I want to write a trigger for Opportunity If i select stagename Close Lost, then a new case should be registered automatically with subject and description field from opportunity . I am Confused little bit about this case Plzz help: Thanx in advance

Best Answer chosen by Rahul Singh 1384
Akshay_DhimanAkshay_Dhiman

Hi Rahul,

As per your requirement:
  •  I have written trigger on Opportunity
  •  Events I have used:
  1. Before Insert
  2. Before update
 
//Trigger Code

trigger NewCase_Trigger on Opportunity (before insert,before update) {
	if(trigger.IsInsert && trigger.IsBefore){
    NewCase_Handler.createCase(trigger.new);
    }
	
      if(trigger.IsUpdate && trigger.IsBefore){
      NewCase_Handler.createCase(trigger.new);
	}
}

*Here is the Handler for above trigger, in which we are implementing the logic
    to create a new case as per the two fields(subject & description) of opportunity.
* If any doubt, feel free to ask.

//Trigger Handler

public class NewCase_Handler {
	public static void createCase(List<Opportunity> opplist)
	{
    	List<Case> caselist=new List<Case>();
   	 
    	for(Opportunity opp: opplist)
    	{
        	if(opp.StageName=='Closed Lost')
        	{
            	Case cs=new Case();
            	cs.Origin='Phone';
            	cs.Status='Working';
            	cs.Description=opp.Description;
            	cs.Subject=opp.Subject__c;
            	caselist.add(cs);
        	}       	 
    	}
    	insert caselist;
	}
}

Hope this may help you!
Regards,
Akshay.

 

All Answers

Akshay_DhimanAkshay_Dhiman

Hi Rahul,

As per your requirement:
  •  I have written trigger on Opportunity
  •  Events I have used:
  1. Before Insert
  2. Before update
 
//Trigger Code

trigger NewCase_Trigger on Opportunity (before insert,before update) {
	if(trigger.IsInsert && trigger.IsBefore){
    NewCase_Handler.createCase(trigger.new);
    }
	
      if(trigger.IsUpdate && trigger.IsBefore){
      NewCase_Handler.createCase(trigger.new);
	}
}

*Here is the Handler for above trigger, in which we are implementing the logic
    to create a new case as per the two fields(subject & description) of opportunity.
* If any doubt, feel free to ask.

//Trigger Handler

public class NewCase_Handler {
	public static void createCase(List<Opportunity> opplist)
	{
    	List<Case> caselist=new List<Case>();
   	 
    	for(Opportunity opp: opplist)
    	{
        	if(opp.StageName=='Closed Lost')
        	{
            	Case cs=new Case();
            	cs.Origin='Phone';
            	cs.Status='Working';
            	cs.Description=opp.Description;
            	cs.Subject=opp.Subject__c;
            	caselist.add(cs);
        	}       	 
    	}
    	insert caselist;
	}
}

Hope this may help you!
Regards,
Akshay.

 
This was selected as the best answer
Rahul Singh 1384Rahul Singh 1384
Thank you Akshay, It works fine
Akshay_DhimanAkshay_Dhiman
Thank you Rahul for selecting my answer as best. It's my pleasure to help you.