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
BeatBeat 

How to find a Process Aproval instance to perform the 'Approve' action

Hi, I'm trying to perform an approval action through APEX code, in an extended controller.  

First I do a Submit for Approval, overwriting the standard one with a visualforce custom button. Here is the code in my extended controller:

 

public PageReference submitApproval() { // Create an approval request for the account Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); req1.setComments('Submitting request for approval.'); req1.setObjectId(purchaseRequisitionObj.id); // Submit the approval request for the account Approval.ProcessResult result = Approval.process(req1); return Page.PR_Detail_VF; }

After that event, the object left in "Pending" approval state. Then in my detail visualforce page, for specific cases, I have a "Direct Approval" button. That button should perform the instance approval changing its state to "Approved". Here is where I'm stuck, because I can not find the way to get the WorkitemId of the request. I mean the workitemId of the instance I asynchronously approved before. This is the code:

 

public PageReference directApproval() { // Instantiate the new ProcessWorkitemRequest object and populate it Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest(); req2.setComments('Direct approval by requester'); req2.setAction('Approve'); req2.setNextApproverIds(new Id[] {UserInfo.getUserId()}); req2.setWorkitemId("FROM WHERE I CAN GET THIS ID??"); // Submit the request for approval Approval.ProcessResult result2 = Approval.process(req2); return Page.PR_Detail_VF; }

 

Any cue will be highly appreciated. Many thanks in advance Fernando.-

 

JOAAJOAA

When you submit for approval, you find the status "Pending" in ProcessInstanceHistory or ProcessInstanceWorkItem table.

Once the approval is complete you will have the status updated accordingly to "Approved" or "Rejected". And these status are updated by SalesForce.These values are not available till transaction is complete.

 

 

BeatBeat

JOAA, thanks for your answer.

 

Look I could see the tables you mention using the sforce Explorer tool, and undertand the sequence you are detailing. But my issue is that I want to perform the Approval action by APEX code, and for creating the request that performs that state change, I need to indicate the work item id by the method: "req2.setWorkitemId()"

 

Because otherwise I can not find the specific Process Work Item that has to be changed in its status.

 

Let me know I didn't explain myself well.

 

Best regards 

 

 
skmskm

You can get the Id by the following query

 

Select p.ProcessInstance.Status, p.ProcessInstance.TargetObjectId,

p.ProcessInstanceId,

p.OriginalActorId,

p.Id,

p.ActorId From ProcessInstanceWorkitem p

where p.ProcessInstance.TargetObjectId = : object.Id

and p.OriginalActorId =:userid

Sully1Sully1

I am trying something very similar.  If you ever got this working would you post the code?  Thanks!

Victor19Victor19
Hi,

I am working on something similar to what you have. I would really appreciate if you could share your code
narsavagepnarsavagep
[ SELECT Id 
  FROM ProcessInstanceWorkitem
  WHERE ProcessInstance.TargetObjectId = :OBJ_ID
    AND ProcessInstance.Status = 'Pending'
  LIMIT 1 ]

Replace OBJ_ID with the variable holding the Id of the Case (or any object) you are looking to get the pending approval for.