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
stollmeyerastollmeyera 

Error on Approval Process trigger

I have the below trigger in my Org.  It automatically submits an approval to the manager when specific leads are marked as Lead Status - Disqualified.  

 

trigger LeadSubmitApproval on Lead (after update) {
 
ID RepID= [Select ID from Profile where Name = 'MINDBODY Sales Representative' Limit 1].id;
ID SarahID= [Select ID from Profile where Name = 'International Associate Manager' Limit 1].id;

    for (Integer i = 0; i < Trigger.new.size(); i++) {
 
        if (Trigger.old[i].Status != 'Disqualified' && 
        	Trigger.new[i].Status == 'Disqualified' && 
        	(UserInfo.getProfileID() == RepID || UserInfo.getProfileId() == SarahID) && 
        	(Trigger.new[i].Lead_Type__c == 'Inbound' || Trigger.new[i].LeadSource == 'Trade Show')) {

            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted DQ Lead for approval. Please approve.');
            req.setObjectId(Trigger.new[i].Id);
            
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            System.assert(result.isSuccess());
            
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
 
        }
 
    }
 
}

 

 

It has been working great for a few months, but has all of the sudden it is throwing the below error:

 

Apex script unhandled trigger exception by user/organization: 00560000001Np28/00D60000000J1xm 

LeadSubmitApproval: 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.LeadSubmitApproval: line 19, column 45

 

Can anyone help me understand why this all of the sudden started throwing errors ??  (Line 19, col 45 is highlighted in red above)

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Next things to check:

 

1. Formula fields that may be updating the record or additional triggers that cause a recursive update thus causing the approval to be submitted twice.

 

 

All Answers

Starz26Starz26

If nothing has changed in the trigger then:

 

Another process is submitting the record befor that specific trigger is ran. Did you copy the trigger, write a new one, etc...... Did you update the trigger with anything?

 

 

 

The error is basically telling you that the record has already been submitted and is awaiting approval or rejection.

 

stollmeyerastollmeyera

That was my first hunch as well, but we have no other approval processes setup.  In fact, if you go to the record itself, there are no approval processes assigned, nor is the record locked.  

Starz26Starz26

Without seeing full debug logs etc, it is hard to tell.

 

Below is the code for my approval process. You can add in the ID's etc where you need to. It is bulkified and check to see if the record is already in an active process....

 

See if it works for you..

 

 //Fire off Approval Process
        
        ProcessInstance[] oPIs = [select Id, TargetObjectID from ProcessInstance WHERE  TargetObjectId IN :trigger.new];
        Approval.ProcessSubmitRequest[] lRequests = New Approval.ProcessSubmitRequest[]{};
        
        Map<id, ProcessInstance> mPI = New Map<id, ProcessInstance>();

        //Build Map
        For(ProcessInstance gPI : oPIs)
            mPI.put(gPI.TargetObjectID, gPI);


        //For each record in the Is After (since this is not an is before)  
        For(OBJECT__c oObj : trigger.new){
    
            //If status is submitted then do the approval process
            if (oObj.FIELD__c == 'Submitted') {   
    
                //If there is not currently an active approval process for this OBJ
                if(mPI.get(oObj.ID) == NULL){    //You should be able to change this to containsKey() - Have not updated my code yet 
                    // create the new approval request to submit            
                    Approval.ProcessSubmitRequest req   = new   Approval.ProcessSubmitRequest();            
                    req.setComments('Submitted for Approval');            
                    req.setObjectId(oObj.Id);          
                    
                    lRequests.add(req);
                    
                }    
            } 
                
        }
        
        if(lRequests.size()>0){                         
            // submit the approval request for processing          
            Approval.ProcessResult[] results =Approval.process(lRequests);    
            
            for(Approval.ProcessResult pResult: results ){
            // display in debug log if the request was successful               
                if(pResult.isSuccess())
                    System.debug('Submitted for approval successfully:  ');
                else
                     System.debug('Submitted for approval failed:  ');
            }
        }
        
    }//End AFTER INSERT
        
    

 I changed my Object to OBJECT and field that is checked to FIELD

 

Modify to suit your needs

stollmeyerastollmeyera

I was actually looking at my debug logs and it appears it is submitting the request twice.  If you have any ideas why this would be happening, I'd love to hear your input.  I am looking into it separately tho.  

Starz26Starz26

Next things to check:

 

1. Formula fields that may be updating the record or additional triggers that cause a recursive update thus causing the approval to be submitted twice.

 

 

This was selected as the best answer
stollmeyerastollmeyera

That was exactly it!!!  I thought back about all the changes I had made and couldn't think of anything that overlapped with this error, but I guess I didn't think hard enough. 

 

Thank you a lot!!!!