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
BewitchedBewitched 

Approval Process

Hi,

 

I am working on an Approval Process and I would like to know who has approved or Rejected using Apex code.

 

 

Thanks,

 

 

Navatar_DbSupNavatar_DbSup

Hi
Try the below code snippet as reference:

 


..................Trigger example.................
trigger OpportunitySubmitForApproval on Opportunity (after update) {

for (Integer i = 0; i < Trigger.new.size(); i++) {

if (Trigger.old[i].Probability < 30 && Trigger.new[i].Probability >= 30) {

// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(Trigger.new[i].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());

}

}

}


..........................................Apex controller example.................................

public class TestApproval {
void submitAndProcessApprovalRequest() {
// 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. 

 

 

BewitchedBewitched

Thanks for a quick reply.

 

I have seen the standard approval class and code that we can use to submit an approval process for a given object

 

However, my scenario is that I have to get the actual approver of a given record (which is already Approved or Rejected). Hence, I do not have to submit the record for approval but merely retrieve the person who as approved the record.

 

Hope this clarifies the scenario in detail.


Regards

Tulika