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
SRILAKSHMI BSRILAKSHMI B 

Approve/Reject the record in apex class

Hi All,

 

I have custom Submit For Approval, Approve and Reject Buttons on the record detail page.

 

I have created a Approval Process .I want to call this Approval Process to execute upon clicking custom 'Submit for Approval' Button.This I am able to acheive by following code.

 

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(recordid);

 

Now when user clicks on  custom 'Approve' button the approval request record created above should be approved.

 

If Can anyone share a sample of code to acheive this it will be helpful.

 

Thanks,

Srilakshmi B

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Here is sample code

Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve'); //This is the action that is approve in your case, you can set it to Reject also
req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

// Use the ID from the newly created item to specify the item to be worked  
    
req2.setWorkitemId(newWorkItemIds.get(0));

// Submit the request for approval  
    
Approval.ProcessResult result2 =  Approval.process(req2);

 You can see comple example here :  http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

All Answers

Shashikant SharmaShashikant Sharma

Here is sample code

Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve'); //This is the action that is approve in your case, you can set it to Reject also
req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

// Use the ID from the newly created item to specify the item to be worked  
    
req2.setWorkitemId(newWorkItemIds.get(0));

// Submit the request for approval  
    
Approval.ProcessResult result2 =  Approval.process(req2);

 You can see comple example here :  http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

This was selected as the best answer
Ken KoellnerKen Koellner

Where did -- newWorkItemIds.get(0)  come from in the prior code?

 

 

kamlesh_chauhankamlesh_chauhan

Go through the URL.

 

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

 

You will find your answer there.

 

Regards,

Kamlesh

Ken KoellnerKen Koellner

The problem with that example is that is shows how it's done if the program is going to create and approve the process in one fell swoop.

 

It doesn't show how to find a process that's pending and approve it.

 

neuronneuron

did any body get an answer for the above question?

rickarnettrickarnett

I stumbled upon this page looking for an answer for this and eventually found the answer at this page: http://shivasoft.in/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/

 

Basically, you want to query out from ProcessInsanceWorkitem for your ObjectID.  This was accomplished by this blogger with the following utility function:

public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;
        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }
        return retVal;
    }

 Hopefully this helps someone else looking for the same info.  A shame that SFDC doesn't include this in the example as I think this case is much more general than doing a submit for approval and appoving all in the same area of code.