All Collections
Feature guides
Comparisons and deployments
Running anonymous Apex code on your orgs
Running anonymous Apex code on your orgs

Run anonymous Apex code on your owned or shared org connections

Quinn Kuiper avatar
Written by Quinn Kuiper
Updated over a week ago

For those teams who have manual pre- or post-deployment steps, executing Anonymous Apex code can be an integral part of your release process.
Previously, you were able to run these apex scripts from within Salesforce, but we now provide the option to run anonymous apex from within Gearset. This allows you to execute one-off scripts either pre-deployment (to prepare some state for the deployment that's about to be made) or post-deployment (to configure or modify something just deployed) depending on your needs.

Note: Execute Anonymous Apex is only supported on the Teams and Enterprise tiers.

This feature is only supported on Orgs that use OAuth connection methods i.e. 'Salesforce' or 'My Domain / Government Cloud' - the 'Username / password' method is not supported.

The apex script execution is done through the SOAP API so the user doing the execution will need both the Author Apex permission and the API Enabled permission.

Running anonymous apex with one of your Salesforce connections

You can run anonymous Apex from the Salesforce Org connection screen (see managing Salesforce org connections).

On a connection, expand the row and select the option to Run Anonymous Apex. This is possible from the, My org connections table, or the Connections shared with me table. For shared connections, you must have been granted delegate-level access.

From here you will be prompted to enter your anonymous Apex code, with an optional description of the script that you've run.

You can Validate the code, this will check that the script will compile correctly but will not execute it.

Click Execute and the result will be shown including any errors in compiling or running the code.

Debugging Logs

As you can see from the screenshot above, you can also view the debug logs for an anonymous Apex execution. By default, the Apex_code logs are returned at DEBUG level, however you can select a specific log category and log category level from the dropdowns.

If you do not want to view the logs, you can toggle them off with the View debug logs toggle or by setting the log category level to None.

Configure Execution

You can also select what you would like to execute when running the Apex tests. This allows you to configure the debug logs that are requested for an execution, therefore you can request the relevant logs accordingly.

Execution History

You can view the history of anonymous Apex executions from within Gearset against your connected orgs by switching to the History tab of the anonymous Apex execution modal.

This allows the user to view who is executing Apex against their org connection, when the code was executed, and what the result of the execution was.

Running anonymous apex associated with one of your deployments

After one of your deployments completes successfully, the deployment success screen allows you to run anonymous apex against the target of the deployment. The button for this can be found in the bottom right of the page.

This will open the anonymous apex modal and any apex executions which you perform from here will be associated with this deployment. History items on the anonymous apex modal will link to this deployment.

When you visit this page from the deployment history, there should also be an indication of the last execution's result status.

If you select the Anonymous Apex tab here, you can see a full listing of all anonymous apex executions which are associated with this deployment.

Did this answer your question?