Deployment Step: Apex

Updated 3 months ago by Copado Solutions

This article is part of the Deployment Steps.

The Apex deployment step lets you execute an Anonymous Apex script in the destination org.

Here you can find some common use cases for this type of deployment step:

  • You can create an Apex step to unschedule scheduled Apex jobs prior to deploying an Apex class. Once you have deployed the Apex class, you can then use a second Apex step to reschedule the modified Apex class.
  • You can use an Apex step to create or modify data after refreshing a sandbox.
  • You can create an Apex step to automatically assign permission sets to specific users once the permission set has been deployed. E.g.:

List<PermissionSetAssignment> permissionSetList = new List<PermissionSetAssignment>();
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'Support_Manager'];
for (User u : [SELECT Id,UserRole.Name,IsActive FROM User WHERE UserRole.Name = 'Support Manager' AND IsActive = true]){
PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = ps.Id, AssigneeId = u.Id);
} insert  permissionSetList;

How to create an Apex deployment step:

  1. Navigate to the Deployments tab and select Advanced (multi-step).
  2. Click on Start Deployment Setup.
  3. Give your deployment a name and select a source and a destination org.
  4. In the Steps section, click on Add Step and select Apex from the Type drop-down menu.
  5. Paste your script in the Apex Script text area and click on Save:

  1. Lastly, click on Deploy to execute the deployment.

How did we do?