Apex Execute

This API executes a Salesforce Apex code block as a Test Step for the defined Salesforce connection. This is useful for triggering a batch job to run immediately.

In Salesforce, Apex Execute is sometimes known as Anonymous Blocks. Refer to the Apex Developer Guide for more information.

Locate the Apex Execute API in the API Palette and drag it into the Test Case:

Update the following parameters:

  • Connection Name: Choose the Salesforce connection for which the Apex code needs to be executed
  • Apex Code: Write or paste the Salesforce Apex Code block you wish to execute. Retain the syntax as per standard Salesforce Apex
  • Result Name: The string returned from Salesforce on running the Apex code
  • Result Scope: The scope of use of this output variable
  • Regular Expression: Filters the result string output based on this regular expression

Then save the Test Case.

Apex Execute Example

The test case in the screenshot above updates the ‘title’ field on contacts in Salesforce by executing the Apex code block. Note the use of Regular Expression in the Results section to extract the result of the Apex query which gives a ‘Success’ on successfully updating the contacts. This in turn is asserted in Provar, and the test is failed if a ‘Success’ is not received as output of the Apex code.

Sample Apex Code

List conlist = [Select Name from Contact where AccountId != null];
for(Contact con : conlist)
con.title = ‘Dr’;
if (conlist.size()>0)
update conlist;
system.debug(‘—-Success, ‘+conlist.size()+’ contact records updated—-‘);


‘\’ should be added immediately before ‘{‘ whenever you run in Apex Execute, as demonstrated in lines 3 and 7 of the sample above. This is necessary because ‘{‘ in Provar are generally used to define variables.