I have a generic piece of code that is responsible of approve a record. It does not assume the SObject type nor the process definition.

For example, something like the following:

public static Boolean approveRecord(Id recordId, String comment, User nextUser) {
  ProcessInstanceWorkitem workItemId = [
    FROM ProcessInstanceWorkitem
    WHERE ProcessInstance.TargetObjectId = :recordId

  Approval.ProcessWorkitemRequest appRequest = new Approval.ProcessWorkitemRequest();
  appRequest.setNextApproverIds(new List<Id>{nextUser});
  Approval.ProcessResult result = Approval.process(appRequest);
  return result.isSuccess();

I want to test this piece of code without losing generability. It was already asked on salesforce.SE how to create an approval process definition programmatically within test classes without success. My thought is that maybe is possible to mock the behavior.

  • selector and service classes; then apexmocks would be an approach. See Fawcett's Salesforce Lightning Platform Enterprise Architecture, 3rd edition
    – cropredy
    Feb 11, 2021 at 21:30
  • @cropredy I had a look at Andy's approach. It's really interesting but I think that is a bit overkill to implement it just for this one case. Also, it's still not very clear how could I be able to mock it even by using it.
    – gvgramazio
    Feb 16, 2021 at 9:31


You must log in to answer this question.

Browse other questions tagged .