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
Anthony PiaiaAnthony Piaia 

I need a trigger that will create check list records automatically depending on the picklist value of an opportunity when the opportunity is created?

I have created an object named Application_Check_List__c  which should create a record automatically based on the value i have in an opportunity picklist (Program_of_Study__c). Within the Application Check list object i created a Master detail relationship with Opportunity.

What i am looking to do is when an opportunity is created we have a picklist which has the program of study and based on the program of study their are a list of items that should be created under the Application checklist object.

Example:

If Program of study MSHRM

Insert checklist item 1
insert checklist item 2
insert checklist item 3

If program of study BBA online

Insert checklist item 1
insert checklist item 2  etc.

Please help as i am new to Apex Triggers
Bhawani SharmaBhawani Sharma
Can you please post what you have been created so far and where you are stucked?
AshlekhAshlekh
Hi,

You need to create check list object record when a opportunity is create so you need to follow below step.

1)  Create a new Trigger on opportunity.

rigger MileageTrigger on Opportunity(after insert) {
   OpportunityHandler.AfterCreation(Trigger.new); 
}
public class OpportunityHandler{
  static void AfterCreation(List<Opportunity> newOppList)
  {
	List<Application_Check_List__c > newApplicaitonList = new List<Application_Check_List__c>();  
	for(Opportunity o : newOppList)
	{
		if(o.Program_of_Study__c == 'MSHRM')
		{
			newApplicaitonList.add(new Application_Check_List__c(provide your fields here));	
			newApplicaitonList.add(new Application_Check_List__c(provide your fields here));	
			newApplicaitonList.add(new Application_Check_List__c(provide your fields here));	
		}
		if(o.Program_of_Study__c == ' BBA online')
		{
			newApplicaitonList.add(new Application_Check_List__c(provide your fields here));	
			newApplicaitonList.add(new Application_Check_List__c(provide your fields here));	
			
		}		
	}
	if(newApplicaitonList.size()>0)
		insert newApplicaitonList;
  }
}

Please follow best Practices

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
 
Anthony PiaiaAnthony Piaia
I forgot to include that the checklist items will need to populate a subjet field i created which is a picklist with all the values.

Example:

if MSHRM then all the lists will be created with the subject values below:

Application Fee
Holds
Funding Plan
FL Residency Proof 1
FL Residency Proof 2
Official UGRD Transcripts
Official GRAD Transcripts
Official Transcripts Translation
Proof of Degree
GRE/GMAT Scores
TOEFL / IELTS Scores
Resume
Statement of Purpose
Letter of Recommendation 1
Letter of Recommendation 2
Admission Interview
Critical Thinking Assessment

So when i look in the opportunity under the Application checklist i should see 17 items with a subject value of each above.
Anthony PiaiaAnthony Piaia
here is what i put in

trigger CreateChecklistbyProgramofStudy on Opportunity (after insert) {
OpportunityHandler.AfterCreation(Trigger.new);
}
public class OpportunityHandler{
   static void AfterCreation(List<Opportunity> newOppList)
   {
     List<Application_Check_List__c > newApplicaitonList = new List<Application_Check_List__c>();
     for(Opportunity o : newOppList)
     {
         if(o.Program_of_Study__c == 'MSHRM')
         {
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Application Fee';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Holds';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Funding Plan';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'FL Residency Proof 1';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'FL Residency Proof 2';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official UGRD Transcripts';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official GRAD Transcripts';));    
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official Transcripts Translation';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Proof of Degree';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'GRE/GMAT Scores';));    
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'TOEFL / IELTS Scores';));     
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Resume';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Statement of Purpose';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Letter of Recommendation 1';));     
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Letter of Recommendation 2';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Admission Interview';));    
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Critical Thinking Assessment';));     
         }
         if(o.Program_of_Study__c == 'MSIRE')
         {
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Application Fee';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Holds';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Funding Plan';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'FL Residency Proof 1';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'FL Residency Proof 2';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official UGRD Transcripts';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official GRAD Transcripts';));    
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Official Transcripts Translation';));   
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Proof of Degree';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'GRE/GMAT Scores';));    
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'TOEFL / IELTS Scores';));     
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Resume';));  
             newApplicaitonList.add(new Application_Check_List__c(Subject__c = 'Statement of Purpose';));  
             
         }     
     }
     if(newApplicaitonList.size()>0)
         insert newApplicaitonList;
   }
}
Anthony PiaiaAnthony Piaia
i have another question, do i need to create a class for opportunityHandler?  also i am getting a sytnax error where it says public class opportunityhandler it saying missing EOF at 'public'
Bhawani SharmaBhawani Sharma
Yes. First 3 lines from Asslekh code snippet is trigger and then a helper class for further processing.