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
skiptomylou11skiptomylou11 

Trigger for auto-submission to Approval Process

Dear all,

 

I am very new to triggers.

I want all opportunities to be automatically submitted for approval if...

 

a) Their Stage is changed to either 'Won' or 'Lost'

b) They are created with the Stage set to 'Won' or 'Lost'

 

What happens based on the below code is that a newly created Oppty with Stage 'Won' or 'Lost' enters approval automatically.

 

When changing the stage to 'won' or 'Lost' on an existing opportunity I get the following error:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunitySubmitForOrderListApproval caused an unexpected exception, contact your administrator: OpportunitySubmitForOrderListApproval: 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.OpportunitySubmitForOrderListApproval: line 12, column 1".

 

Moreover creating a new oppty with the Stage NOT set to 'Won' or 'Lost'  brings the following error

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunitySubmitForOrderListApproval caused an unexpected exception, contact your administrator: OpportunitySubmitForOrderListApproval: 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.OpportunitySubmitForOrderListApproval: line 12, column 1". 

 

Any help is greatly apprecated!!! 

Thanks! 

 

trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) {
 
    for (Integer i = 0; i < Trigger.new.size(); i++) {
 
        if (Trigger.old[i].StageName <> '6 - Project won'  ||  Trigger.old[i].StageName <> '7 - Project lost'  &&   Trigger.new[i].StageName == '6 - Project won'  ||  Trigger.new[i].StageName == '7 - Project lost') {
 
            // 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());
 
        }
 
    }
 
}

 

ignatiuz_forceignatiuz_force

The first case seems to occur due to human error. Here is what is happening probably:

1) you edit the status to won or lost on existing opp and trigger is executed. The record gets submitted for approval.

2) then you are trying to edit the same record again by changing the status. So the trigger is executed again but because the record is already under approval, it cannot be submitted again.

 

As for the second one, you have to change the if statement, parenthesis around and condition is missing

if (Trigger.old[i].StageName <> '6 - Project won'  ||  Trigger.old[i].StageName <> '7 - Project lost'  &&   (Trigger.new[i].StageName == '6 - Project won'  ||  Trigger.new[i].StageName == '7 - Project lost')) 
skiptomylou11skiptomylou11

Thanks Mukul,

 

The issue with 2nd error is indeed solved. Many thanks.

 

For the first error however I don't think it is caused by a second submission.

 

I create an opportunity with the Stage set to 'New Opportunity and save it.

At this set nothing is submitted to approval. I check the 'Approval History' related list.

Than I edit the Stage to 'Won' and click save and it brings up the error.

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunitySubmitForOrderListApproval caused an unexpected exception, contact your administrator: OpportunitySubmitForOrderListApproval: 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.OpportunitySubmitForOrderListApproval: line 12, column 1".

 


Below is the updated trigger:

trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) {
 
    for (Integer i = 0; i < Trigger.new.size(); i++) {
 
        if ((Trigger.old[i].StageName <> '6 - Project won'  ||  Trigger.old[i].StageName <> '7 - Project lost')  && (Trigger.new[i].StageName == '6 - Project won'  ||  Trigger.new[i].StageName == '7 - Project lost')) {
 
            // 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());
 
        }
 
    }
 
}

 

ignatiuz_forceignatiuz_force

Can you check in developer console if there is any other trigger/code running along with your trigger ?or setup the debug log and mail it, I'll have a look.

 

 

neophyteneophyte

This could be because the after update trigger getting called again (after a field update from workflow).

 

Trigger order of execution on update: before update trigger -> after update triggers -> workflows -> (if any field updates) -> before update trigger -> after update trigger.

 

So if your trigger runs the second time, it tries to submit the record, which was already submitted for approval, a second time. This results in the error. Please check.

 

- Anand  

Ruwantha LankathilakaRuwantha Lankathilaka

Make sure you approval process is Activated.