1

This guide instructs how to configure an approval process through Salesforce..

https://help.salesforce.com/articleView?id=approvals_creating_approval_processes.htm&type=5

however I don't see any documentation for completing this programatically. My motivation is I'd like to unit test some approvals and need to use apex to create the approval process on an object and THEN create approval requests and test my methods on those requests.

4
  • 2
    When you say 'create', do you mean 'submit a record into the approval process'? You can't create the process itself without the Metadata/Tooling APIs, neither of which you can call in a unit test. – David Reed Jul 10 '18 at 21:55
  • Not submit a record -- I mean actually define it. So could i then package metadata with an Approval Process define and THEN run code to test against it? I think that could work. – Shuaib Ahmed Jul 11 '18 at 14:53
  • Yes, you can package the Approval Process metadata and include Apex unit tests in the package that make use of it (see Renato's answer for more). – David Reed Jul 11 '18 at 16:08
  • I think what you are saying more directly answers the question. If you have any more to share about packaging an Approval Process that'd be great. I could accept the answer below. Thanks! – Shuaib Ahmed Jul 16 '18 at 14:45
2

Part of what you want can't be done programatically. When you say "create" we understand that you want to set up an approval process (defining which record kind you want to approve, the steps to approve, entry criteria, etc). You can't do that.

Now, if you want to unit test the approval process itself, you can do so in Apex using the classes inside the Approval class.

Here you can find an example on how to test an approval process in a unit test. Note that on line 19 you have the following statement:

req1.setProcessDefinitionNameOrId('PTO_Request_Process');

This will tell the platform which approval process you want to run for that record. This is the API name of the approval process you create through the user interface.

0

I think Renato provided helpful details, but I'll add a little more detail based on my comments per request.

Approval Processes are defined as metadata. As such, they cannot be created in normal Apex code. While it's possible to make a callout in Apex to the Metadata API to create and insert an Approval Process (using, for example, the apex-mdapi library), this is not possible in a test context. Tests are never to have side effects that persist beyond their execution, and you cannot make callouts in a test context at all.

In most cases, your code is not expected to mock or generate metadata upon which it depends. (One major exception to that statement is Custom Metadata records, which are intended for providing deep customization of code and can require mocking in unit tests).

If your code depends on the presence of this approval process, you should likely deploy the code and the associated metadata in a single operation, whether that is a change set or a package deployed via Salesforce DX, the Force.com Migration Tool (Ant), Workbench, or some other Metadata API-based tool. You can include the approval process in your package.xml, and must also include any other metadata that the approval process depends on - like custom fields, workflow updates, and email templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.