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
Deepak PansariDeepak Pansari 

Approval re-assign via apex code..

Hi All,

 

I need to re-assign Approval to some one else via apex code , when an object is already submitted for approval and status is pending.

 

Any help ?

 

Deepak

 

Ispita_NavatarIspita_Navatar

Check the  below especially the highlighted part which shows the syntax for set ApprovarID:-

public class testApproval {

{

// Insert an account

Account a = new Account(Name='Test',annualRevenue=100.0);
insert a;

// Create an approval request for the account

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(a.id);

// Submit the approval request for the account

Approval.ProcessResult result = Approval.process(req1);

// Verify the result

System.assert(result.isSuccess());
System.assertEquals('Pending', result.getInstanceStatus(), 'Instance
Status'+result.getInstanceStatus());

// Approve the submitted request
// First, get the ID of the newly created item

List<Id> newWorkItemIds = result.getNewWorkitemIds();

// Instantiate the new ProcessWorkitemRequest object and populate it

Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve');
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);

// Verify the results

System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
System.assertEquals('Approved', result2.getInstanceStatus(), 'Instance
Status'+result2.getInstanceStatus());
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

Sandeep123Sandeep123

Hi Dipak

 

Use list of "ProcessInstanceWorkItem" ( Method below)

 

public pagereference reassign(){

 

List<ProcessInstanceWorkItem> workItemList = [Select p.ProcessInstance.Status, p.ProcessInstance.TargetObjectId,p.ProcessInstanceId,p.OriginalActorId,p.Id,p.ActorId
        From ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :  "Your record id"];

 

workItemList.get(0).ActorId  = 'id of user who will be reassigned';

 

update workItemList;

 

return null;

 

}

 

it will automaticlly assigned to that id and update it in Approval history also.

 

it is working for me, let me know if it work for you or not.