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
RejonesRejones 

Apex Trigger for Approval Process

I am trying to get an opportunity to auto submit for an approval process called Incentive offer when the field Incentive offer in the oppportunity is Yes.

 

I build the following but keep getting an error message error: expecting a right parentheses, found 'Offer' at line 5 column 30

 

 

trigger OpportunitySubmitForApproval on Opportunity (after update) { 

 

     for (Integer i = 0; i < Trigger.new.size(); i++) {        

 

  if (Trigger.Incentive Offer Yes) {             

 

// create the new approval request to submit      

Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();     

req.setComments('Submitted for approval. Please approve.');

req.setObjectId(Trigger.new[i].Id);           

// submit the approval request for processing           

Approval.ProcessResult result = Approval.process(req);           

// display if the reqeust was successful           

System.debug('Submitted for approval successfully: '+result.isSuccess());         

 

              }     

 

         }

 

}

Best Answer chosen by Admin (Salesforce Developers) 
cml26sepcml26sep

You just need to move out the declaration of 'approvalReqList' outside for loop. Something like this:

 

trigger OpportunitySubmitForApproval on Opportunity (after update) {

List<Approval.ProcessSubmitRequest> approvalReqList=new List<Approval.ProcessSubmitRequest>();
 

for (Oppertunity opp: Trigger.New)

{       

  if (opp.Incentive_Offer__c=='Yes')

 {           

     // create the new approval request to submit    

   Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();   

   req.setComments('Submitted for approval. Please approve.');

   req.setObjectId(opp.Id);

   approvalReqList.add(req);        

   }   

  }

 

// submit the approval request for processing        

List<Approval.ProcessResult> resultList = Approval.process(approvalReqList);        

// display if the reqeust was successful

for(Approval.ProcessResult result: resultList )

{        

System.debug('Submitted for approval successfully: '+result.isSuccess());      

}

 

}

 

All Answers

cml26sepcml26sep

Modify you trigger something as below :

 

trigger OpportunitySubmitForApproval on Opportunity (after update) {

 for (Oppertunity opp: Trigger.New)

{        

  if (opp.Incentive_Offer__c=='Yes')

 {            

  List<Approval.ProcessSubmitRequest> approvalReqList=new List<Approval.ProcessSubmitRequest>();

   // create the new approval request to submit     

   Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();    

   req.setComments('Submitted for approval. Please approve.');

   req.setObjectId(opp.Id); 

   approvalReqList.add(req);         

   }    

  }

 

// submit the approval request for processing         

List<Approval.ProcessResult> resultList = Approval.process(approvalReqList);         

// display if the reqeust was successful

for(Approval.ProcessResult result: resultList )

{         

System.debug('Submitted for approval successfully: '+result.isSuccess());       

}

 

}
....

 

I hope this helps.

RejonesRejones

Thank you for your reply I tried the formula that you sent and I got an error message that says

ErrorError: Compile Error: Variable does not exist: approvalReqList at line 31 column 60
cml26sepcml26sep

You just need to move out the declaration of 'approvalReqList' outside for loop. Something like this:

 

trigger OpportunitySubmitForApproval on Opportunity (after update) {

List<Approval.ProcessSubmitRequest> approvalReqList=new List<Approval.ProcessSubmitRequest>();
 

for (Oppertunity opp: Trigger.New)

{       

  if (opp.Incentive_Offer__c=='Yes')

 {           

     // create the new approval request to submit    

   Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();   

   req.setComments('Submitted for approval. Please approve.');

   req.setObjectId(opp.Id);

   approvalReqList.add(req);        

   }   

  }

 

// submit the approval request for processing        

List<Approval.ProcessResult> resultList = Approval.process(approvalReqList);        

// display if the reqeust was successful

for(Approval.ProcessResult result: resultList )

{        

System.debug('Submitted for approval successfully: '+result.isSuccess());      

}

 

}

 

This was selected as the best answer
RejonesRejones

That worked thank you for your help. 

RejonesRejones

Chandra

 

I hate to bother you on this silly question but how can I add a record type to this Apex Trigger.  It worked fine just like you have it until I had to add a record type for the opportunity page. 

 

Can you tell me what to add to you formula for record type?

Jerun JoseJerun Jose

You can simply add another check inside your for loop for record type ID check.


Change

if (opp.Incentive_Offer__c=='Yes')

 to

if (opp.Incentive_Offer__c =='Yes' &&
	opp.RecordTypeID == 'xxxxx')

 

RejonesRejones

That worked perfect, Your Awesome, Thank you so much.

RejonesRejones

This trigger worked great in the sand box after I added the record type for paycard.  Now I am trying to move it to production and I am getting the following error message.  

 

Test Coverage of selected Apex trigger is 0% at least 1% test coverage is required. 

 

Salesforce is telling me that I have to build a test class for this trigger to test it.

 

Can you help me with this. 

cml26sepcml26sep
That's correct you have to write a test class to test the trigger code and increase code coverage.