+ Start a Discussion
RadDude89RadDude89 

Apex trigger to start standard approval process

Hi,

We have set up an approval process called Renew.  This approval process was created via the standard process (adding email alerts, approval/rejection field updates, etc). When we create an offer record the approval process doesn't start - the user must press submit for approval.  Can you create an apex trigger to initiate an approval process when a record has been created?
So in a way we would be pressing the submit for approval for the user automatically.
Can the trigger call on the ID of the approval process to initiate it if certain requirements are met?

Best Answer chosen by RadDude89
hitesh90hitesh90
Hello,

You can try below sample code for your requirement.

Apex Trigger:
trigger callApprovalProcess on Offer__c (after insert) {
    for(Offer__c ofr: trigger.new){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(ofr.id);
        Approval.ProcessResult result =  Approval.process(req);
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/

All Answers

VineetKumarVineetKumar
This can be done using the salesforce point and click - Process Builder
http://releasenotes.docs.salesforce.com/en-us/spring15/release-notes/rn_forcecom_process_visually_automate.htm#rn_forcecom_visually_automate

Still if you want to do it with a trigger, you get the sample code with test class from below link:
http://blog.jeffdouglas.com/2010/01/04/automating-salesforce-approval-processes-with-apex-triggers/
hitesh90hitesh90
Hello,

You can try below sample code for your requirement.

Apex Trigger:
trigger callApprovalProcess on Offer__c (after insert) {
    for(Offer__c ofr: trigger.new){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(ofr.id);
        Approval.ProcessResult result =  Approval.process(req);
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
This was selected as the best answer
RadDude89RadDude89
Thanks guys that works, sorry I was getting confused why the approval process wasn't triggering correctly but I was referencing the parent object instead of child