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
AnshiiAnshii 

Approve automatically using trigger

Hi everyone , I have written a trigger so for for submitting a record for approval.i need to set the trigger to approval automatically please help me out.
Trigger:1

trigger contactcreated on Contact (after insert) {
    for(contact c : trigger.new){
        if(c.isprimary__c == True){
            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(c.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());
        }
    }
}
Trigger2:
trigger Contactcreatedapprocess on Contact (after insert) {
     user u1=[select id from user where alias='anshu'];
    for(contact c:Trigger.new){
        if(c.isprimary__c==true){
        //step1:create a approval req to submit
       Approval.ProcessSubmitRequest req=new Approval.ProcessSubmitRequest();
        req.setComments('submitted for approval please submit');
        req.setObjectId(c.Id);
        req.setSubmitterId(u1.id);
       //submit the request for approval
      Approval.ProcessResult result=Approval.process(req);
    }
    }
}

Thanks in Advance
Best Answer chosen by Anshii
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You have to handle the different cases as approved, reject.
Please follow the below link as everything is given in detail:-  
https://salesforcescool.blogspot.com/2018/11/submit-approval-process-using-apex.html

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,

You have to handle the different cases as approved, reject.
Please follow the below link as everything is given in detail:-  
https://salesforcescool.blogspot.com/2018/11/submit-approval-process-using-apex.html

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
This was selected as the best answer
AbhinavAbhinav (Salesforce Developers) 
Hi Arroju,

Have you check this link which is similar to your use case:

https://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/

If it helps please mark it as best answer.

Thanks!
sakhisakhi
Hi Anusha ,

Where are you getting the approver from?
 
If it is a related user, can you set the criteria in the approval process that if the field is != that value put in the process else approve record?
 
Otherwise, you will have to write a trigger on the object that was actually submitted for approval
 
Here is some code I use in a test method to approve a record
 
//Approve the record
            ProcessInstance pi = [Select ID, Status, TargetObject.Name 
                From ProcessInstance 
                Where TargetObjectID = :IDOFRECORD AND Status = 'Pending'];

            if(pi !=Null){
                ProcessInstanceWorkitem piwi = [select Id,OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId= :pi.Id LIMIT 1];
                Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
                prWkItem.setWorkItemID(piwi.id);
                prWkItem.setComments('Auto Approve');
                prWkItem.setAction('Approve');
                Approval.ProcessResult appResult = Approval.process(prWkItem);
            }


Another nice post to refer
http://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/