Skip to main content
All CollectionsCompare and deploy metadataApex and testing
Automated post-deployment anonymous Apex
Automated post-deployment anonymous Apex

Learn how to run an anonymous Apex script after a successful deployment

Matt Cree avatar
Written by Matt Cree
Updated over 4 months ago

License requirements: Automated Anonymous Apex is only supported on the Teams and Enterprise tiers.

In the Compare and Deploy flow you can attach an anonymous apex script that will run automatically after the deployment completes successfully. By leveraging anonymous Apex, you can perform custom operations, data manipulations, or complex calculations in your Salesforce orgs during deployments.

If instead you wish to run anonymous apex against your org interactively, you can learn more about that here.

It's important to note that failures encountered during the execution of the anonymous Apex script are not considered deployment failures. This distinction ensures that any issues or errors within the script do not impact the overall outcome of the deployment. You can focus on addressing and resolving any script-related errors independently, without affecting the deployment status.

Note: 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

Attaching anonymous Apex to a deployment

You have the ability to attach anonymous Apex scripts to different types of deployments, including:

  • Deployments

  • Scheduled deployments

  • Validated deployments (these will only run after the validated package is finally deployed)

Deployments and Scheduled Deployments

For regular deployments and scheduled deployments, you can find the option to attach anonymous Apex scripts under the Issue tracking section of the deployment attributes. Look for the Post-deployment Apex button in that section.

After selecting the option to attach an anonymous Apex script to your deployment, you will be able to specify the script that you want to run when the deployment completes successfully. Simply enter your desired script in the provided field, and then first Validate your Apex script to perform the syntax check within Salesforce and then Close to return to the deployment summary.

Once you have attached the script and closed the dialog, you can proceed to initiate the deployment. The attached anonymous Apex script will be executed automatically after the deployment completes successfully.

Note: No rollback mechanism exists for Anonymous Apex. Apex is a powerful tool so please ensure that your code is correct to avoid adverse effects.

This feature provides a straightforward way to include custom Apex code in your deployment process and ensures that your desired actions are performed seamlessly as part of the deployment workflow.

Validations and validated packages

After a validation completes successfully, you will be directed to the validation succeeded page, where you can access and review the attached anonymous Apex script by selecting the View Post Deployment Script button.

For validated packages, you can view an attached script by selecting the Post-deployment Apex button. When you are working with validated packages, this button allows you to access and review the attached anonymous Apex script associated with the package.

Viewing the results of the anonymous Apex run

After running anonymous Apex as part of your deployment, you can easily view the results in two locations:

  • Deployment success page

  • Anonymous Apex execution history

The details of this are largely the same as running anonymous apex manually and more information can be found here.

Did this answer your question?