You need to sign in to do that
Don't have an account?

Approval process through Apex trigger issue
I am invoking an approval process in an after update trigger which results in bunch of runtime errors. The approval process works flawlessly when submitted through default "Submit for Approval" button
below is my trigger code and the run time error messages
trigger Approval_Process on SPA__c (after update){
for(SPA__c sp: Trigger.new){
if (sp.Submit_for_Approval__c){
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(sp.id);
req1.setNextApproverIds(new Id[] {UserInfo.getUserId()});
//req1.setNextApproverIds='005Q0000000IFNf';
Approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());
System.assertEquals('Pending', result.getInstanceStatus(), 'Instance Status'+result.getInstanceStatus());
}
}
}
The runtime errors I get are
- CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
- NO_APPLICABLE_PROCESS
The detailed message is below
Error: Invalid Data. Review all error messages below to correct your data.Apex trigger Approval_Process caused an unexpected exception, contact your administrator:
Approval_Process: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
Approval_Process: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.: [] Trigger.Approval_Process: line 11, column 45: []: Trigger.Approval_Process: line 11, column 45
Oh well.. I am answering my own question...Hope this will be help to others
I made following 2 changes and the approval process trigger now works as expected...
1- Add an if condition around approval process submission code to match the entry criteria conditions of the approval process (thanks to Srini for his input)
2- added logic to get the next approver Ids from the membership of a public group
The working code now looks like this..
trigger Approval_Process on SPA__c (after update){
//Retrieve approvers Ids i.e. memebers of SPA Approval Queue
list<string> idList = new list<string>();
List<String> mailToAddresses = new List<String>();
group g = [SELECT (select userOrGroupId from groupMembers)
FROM group
WHERE name= 'CCP- NAM - SPA Approvers'];
for (GroupMember gm : g.groupMembers) {
idList.add(gm.userOrGroupId);
}
//Submit records for approval the if condition matches with entry criteria
for(SPA__c sp: Trigger.new) {
if (sp.submit_for_approval__c
&& sp.status__c<>'Approved'
&& sp.status__c <>'Submitted'
&& sp.line_item_count__c >0 ) {
approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(sp.id);
req1.setNextApproverIds(idList);
approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());
System.assertEquals('Pending', result.getInstanceStatus(), 'Instance Status'+result.getInstanceStatus());
}
}
}
All Answers
You might have kept a entry criteria in order to submit the approval process, same condition you need to add in your trigger, otherwise it will give you the error.
Assume you have a condition as status = 'pending approval' then you have to add the same condition in Trigger as well.
Hope this helps.
Thanks for your comments Srini,
I updated the if condition in my trigger to exactly match with the entry criteria of the approval process, still no luck...
The record I am testing with, also meets entry criteria because I can successfully submit the same record for approval using "Submit for Approval" button.
Oh well.. I am answering my own question...Hope this will be help to others
I made following 2 changes and the approval process trigger now works as expected...
1- Add an if condition around approval process submission code to match the entry criteria conditions of the approval process (thanks to Srini for his input)
2- added logic to get the next approver Ids from the membership of a public group
The working code now looks like this..
trigger Approval_Process on SPA__c (after update){
//Retrieve approvers Ids i.e. memebers of SPA Approval Queue
list<string> idList = new list<string>();
List<String> mailToAddresses = new List<String>();
group g = [SELECT (select userOrGroupId from groupMembers)
FROM group
WHERE name= 'CCP- NAM - SPA Approvers'];
for (GroupMember gm : g.groupMembers) {
idList.add(gm.userOrGroupId);
}
//Submit records for approval the if condition matches with entry criteria
for(SPA__c sp: Trigger.new) {
if (sp.submit_for_approval__c
&& sp.status__c<>'Approved'
&& sp.status__c <>'Submitted'
&& sp.line_item_count__c >0 ) {
approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(sp.id);
req1.setNextApproverIds(idList);
approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());
System.assertEquals('Pending', result.getInstanceStatus(), 'Instance Status'+result.getInstanceStatus());
}
}
}
hi,
when using trigger to start the approval process.should the approval process be deactivated.
because when i am running the trigger making it active i am getting the following exceptions
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
ALREADY_IN_PROCESS
if i make it deactivate i get the following error:
NO_APPLICABLE_PROCESS, No applicable approval process found.:
please help me solve the issue.
thanks
Hi,
Did you got solution for your Question??
Coz, i'm also facing the same error.
Thanks in Advance.
Hi,
I am trying to set multiple users as approvers and getting a runtime error when I assign multiple ids. Do we need to set anything else to make the multiple ids work?
Thanks.
Hi,
I've created the following trigger with the purpose to auto submit an opportunity for approval. Unfortunetly it dosn't work. Any ideas?
trigger OpportunitySubmitForApproval on Opportunity (after update, after insert) {
for (Integer i = 0; i < Trigger.new.size(); i++) {
if (Trigger.old[i].Probability < 100 && Trigger.new[i].Probability >= 100) {
// 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());
}
}
}
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunitySubmitForApproval caused an unexpected exception, contact your administrator: OpportunitySubmitForApproval: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []: Trigger.OpportunitySubmitForApproval: line 12, column 45".
Looks like you are submitting the same record for approval repeatedly.
Make sure you are excluding such records which are already submitted for approval.
Hope that helps.
Afzal
Please try this.
Hope this will help you.
Hi Yo,
Do you know how to write the apex to exclude records which are already submitted for approval?
Hi Uzair,
Thanks a lot, everything is fine now.
/Henrik
List<Approval.ProcessSubmitRequest> approvalReqList=new List<Approval.ProcessSubmitRequest>();
Approval.ProcessSubmitRequest req;
for(Beneficiary__c ben:Trigger.new){
if (Ben.Total_Contract_Claim_Amount__c >= 1000000) {
req = new Approval.ProcessSubmitRequest();
req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ben.Id);
Approval.ProcessResult result = Approval.process(req);
}
}
}