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());
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?
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.
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
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.
Thank you for your reply I tried the formula that you sent and I got an error message that says
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());
}
}
That worked thank you for your help.
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?
You can simply add another check inside your for loop for record type ID check.
Change
to
That worked perfect, Your Awesome, Thank you so much.
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.