Close Sun Nov 18 14:13:21 GMT 2018

Running Apex code from Apache Ant

Almost every enterprise that automates Salesforce deployments will need to execute Apex code. For example:  test data set up in temporary environments, the assignment of permission sets, or the starting and stopping of scheduled jobs.

Apache Ant is a declarative cross-platform tool for defining build steps as XML. This Ant macro supports running Apex code from any build step using Execute Anonymous. It's self contained and doesn't require external java libraries.

Usage:

First paste this macrodef into your build.xml

Then invoke the macro inside any build target:

<target name="execanon">
    <execanon username="${sf.username}" password="${sf.password}">
        System.debug('Stopping all scheduled jobs');
        for (SObject cron : [SELECT Id FROM CronTrigger])
        System.abortJob(cron.Id);
    </execanon>
</target>

How does it work?

First, the macro obtains a Session ID by logging into Salesforce using the username and password provided in the build properties file. Then it assembles the proper SOAP request to call ExecAnon using the Tooling API.

What if my code contains XML?

Add a <![CDATA[ ... ]]> section around the code. This will escape any < & > characters to entities before assembling the request body.



Related