1

I am using custom button to submit for approval process. In the custom button I am validating lot conditions usin gJavascript API, if everything satifies then it will change the Status = Under approval.

Based on the Status field value, in trigger i am submitting the record for approval process.

trigger code:

if(obj.Status__c == 'Under Approval' && obj.Status__c != oldobj.Status__c) {
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval automatically using Trigger');
    req1.setObjectId(obj.id);
    Approval.ProcessResult result = Approval.process(req1);
}

error Message:

Trigger.Custom_Object_Trigger: line 14, column 1 19:42:19.479 (1479053000)|FATAL_ERROR|System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []

Could you please me know why this error is coming?

2

The reason you're getting this error message is because you cannot submit a record which is already in the approval process. And the reason I think it is getting submitted again is because this trigger is getting called more than once which might be caused by a workflow field update etc.

So to avoid this error message you can use a static variable, set it to false after the records is submitted. Check for this static varaible in the if () condition and that should do the trick.

public class UtilClass{
    public static boolean isSubmitted=false;
}

in your Trigger you can do something like this:

if(obj.Status__c == 'Under Approval' && obj.Status__c != oldobj.Status__c &&  !UtilClass.isSubmitted) {
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval automatically using Trigger');
    req1.setObjectId(obj.id);
    Approval.ProcessResult result = Approval.process(req1);
    UtilClass.isSubmitted = true;
}

From your code it looks like you have not bulkified the trigger for submitting the approvals. I think you should probably look into that as well. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.