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
Julie CurryJulie Curry 

approving a record based on a picklist value updated via an API

Sorry if this is not the right place to post this.

I am creating an approval process and in the last step, I would like the final approval to be based on a picklist value inserted by an API.  If picklist value is "success", then I would like to approve the record, lock the record, email the initial requestor, and update the status of the record to closed.  If picklist value is "fail" then I would like to reject the submission, send an email to the last approver informing him/her of the failure and requesting that they review the error message and make edits where needed, and update the record status to Pending Approval.  

How can I make this happen since I want a value from a field to determine if something is approved, and not a user?  I see that there is an option in Process Builder to submit a record for approval, but I don't see an option to approve.

This is my first time to create an approval process.  Thanks in advance for your help!

 
Arpit Jain7Arpit Jain7
Julie,

You can use apex to approve or reject record dynamically based on picklist field value. You can refer below sample code to approve any record from apex

//Approve the FSA
            ProcessInstance piFSA = [Select ID, Status From ProcessInstance Where TargetObjectID = :pTeam.FSA__c AND Status = 'Pending']                 if(piFSA !=Null){
                ProcessInstanceWorkitem piwiFSA = [select Id,OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId= :piFSA.Id LIMIT 1];
                Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
                prWkItem.setWorkItemID(piwiFSA.id);
                prWkItem.setComments('Auto Approve FSA upon manager assignment of a partner member in .......');
                prWkItem.setAction('Approve');
                Approval.ProcessResult appResult = Approval.process(prWkItem);                
            }

Similarly you can reject any record. Let me know if you need any more help.

Thanks
Arpit