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
RadDude89RadDude89 

Apex trigger to automatically invoke approval process

Hi,

I have created an approval process for a custom object called Offer which is based on several selections regarding the creation of the offer.
The offer is to be locked and sent to a manager for approval when the criteria is
Price <0.200 and Region= NI
What I have realised is that when the offer is created the approval won't start - the user has to press the Submit for Approval for it to start.

Is it possible to create a trigger to kick the approval process off automatically when the offer has been created?

Best Answer chosen by RadDude89
Amit GhadageAmit Ghadage
Hi RadDude89,
You can write trigger as follow,replace with API names as you have.
trigger approveOffer on  Offer__c (after update,after insert) {

    for (Offer__c offer :  Trigger.new) {

        if (offer.Price < 0.200  && offer.Region =='NI' {
            Approval.ProcessSubmitRequest approvalRequest = new Approval.ProcessSubmitRequest();
            req.setComments('Offer Submitted for approval');
            req.setObjectId(offer.Id);
            Approval.ProcessResult approvalResult = Approval.process(approvalRequest);
            System.debug('offer submitted for approval successfully: '+approvalResult .isSuccess());

        }

    }

}
Best Regards,
Amit Ghadage.
 

All Answers

Amit GhadageAmit Ghadage
Hi RadDude89,
You can write trigger as follow,replace with API names as you have.
trigger approveOffer on  Offer__c (after update,after insert) {

    for (Offer__c offer :  Trigger.new) {

        if (offer.Price < 0.200  && offer.Region =='NI' {
            Approval.ProcessSubmitRequest approvalRequest = new Approval.ProcessSubmitRequest();
            req.setComments('Offer Submitted for approval');
            req.setObjectId(offer.Id);
            Approval.ProcessResult approvalResult = Approval.process(approvalRequest);
            System.debug('offer submitted for approval successfully: '+approvalResult .isSuccess());

        }

    }

}
Best Regards,
Amit Ghadage.
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
1) http://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/

Let us know if this will help you
RadDude89RadDude89

Hi,

Thanks for the replies - I've tried using the approval process submitted from Amlt Ghadage but when I compile it, I get the error
Variable does not exist: req at line 7 column 13

How should I declare that variable?

Vivek DVivek D
Hi 
Create a approval proccess on the offer object and put one of the entry criteria as Price < 0.200
Now to trigger the approval process you can use process builder rather than trigger which will save you from writing code.
And if still you want to use trigger check the price in trigger and add the following line of code
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Some Comments');
req.setObjectId(Record.Id);
Approval.ProcessResult result = Approval.process(req);

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
1) http://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/

Full detail and sample code is given in above link

Let us know if this will help you
Amit GhadageAmit Ghadage
instead of req use approvalRequest 
RadDude89RadDude89

Thanks for your help guys I got this working.

But I have another question - is it possible to use a trigger to invoke an already created approval proccess.

So for example - I have created an approval process with various email alerts, field updates (based on approval and rejection).
Can a trigger be developed to call a particular approval process?

So I have an approval process with the Salesforce ID of '04a8E000000DfNn', can you use the code above to call that ID and start the approval once the record has been created?