Delete Metadata Step and Test Level in Deployments

When working with the destructive changes feature, you will notice that, apart from the Git Promotion step, a Delete Metadata step is created in the Deployment record.

User-added image

Git Promotion Step

The Git Promotion step will make the changes in Git and deploy components to the destination org. This step will not make any deletions in the destination org.
The test level in this step can be defined using any of the following options:

  1. When creating a deployment from the Promotion record. The test level selected there will be applied to all the steps in the Deployment record.

  2. With the Promotion Test Level field in the user story if the Promote & Deploy checkbox enabled.

  3. By changing the test level directly in the Git Promotion step in the Deployment record.

If the test level in the Git Promotion step is set to RunSpecifiedTests, the test classes that will run when deploying will be those in the list of components in the deployment, or the ones specified in the Test Classes attachment in the Deployment record, if the button Add Test Classes was used in the user stories.

Delete MetaData Step

The only purpose of the Delete MetaData deployment step is to delete in the destination org the metadata components that were selected in the destructive changes commit.
The test level in this step can be defined using any of the options below:

  1. When creating a deployment from the Promotion record. The test level selected there will be applied to all the steps in the Deployment record.

  2. With the Promotion Test Level field in the user story if the Promote & Deploy checkbox enabled.

  3. By changing the test level directly in the Git Promotion step in the Deployment record.
If you are using Copado v11, this will require some manual steps, since there is no field in the VF page that allows you to change the test level:
  1. Copy the Deployment record Id.
  2. Go to the Home tab.
  3. Remove home/home.jsp from the url.
  4. Paste DeploymentId?nooverride
  5. You are now in the standard layout of the Deployment record, overriding the Visualforce page.
  6. In the Steps related list, click on the Delete Metadata Step Name.
  7. In this page you will see a text field called dataJson. This field contains the test level for the Delete Metadata step. The options are:
  • Blank value. If there is no value in this field, the default test level selected by Salesforce will be automatically applied when deploying.
  • {"testLevel":"NoTestRun"}
  • {"testLevel":"RunSpecifiedTests"}
  • {"testLevel":"RunLocalTests"}
  • {"testLevel":"RunAllTestsInOrg"}
   
 

If you do not enter any value in the dataJson field, Salesforce will execute the Delete Metadata step with the default test level based on the components in the Delete Metadata step and the type of environment to which you are deploying.

What Is the Default Test Level Selected by Salesforce?

 

NoTestRun: This test level is the default test level for development environments.

RunLocalTests: This test level is the default test level for production environments that include Apex classes or triggers.

Default Test Execution in Production

When deploying to production, all tests, except those that originate from managed packages, are executed if your deployment package contains Apex classes or triggers. If your package doesn’t contain Apex components, no tests are run by default.

Please note that it is not the same when Salesforce selects NoTestRun as the default test level when no value is entered in the dataJson field as when you select NoTestRun as the test level for the Delete Metadata step.

If you are trying to delete classes or triggers, the default value selected by Salesforce will be RunLocalTests if you leave the dataJson field blank. This will make all the local test classes to run. If you want to run only some specific test classes you will have to select RunSpecifiedTests as the test level.

If the test level in the Delete Metadata step is set to RunSpecifiedTests, the test classes that will run when deploying will be those specified in the Test Classes attachment in the Delete Metadata step. This file must be created and attached manually in the Delete Metadata Step record. You can access this record following the steps mentioned above.



 

Please make sure the Test Classes file has no extension. If you see an extension in the file once attached, it won’t work.
If the option below is enabled in your Salesforce org, you will have to insert the attachment from the developer console, otherwise you will be adding a file and not an attachment, and the default test level RunLocalTests will be selected by Salesforce instead of RunSpecifiedTests.


User-added image

Below you will find an example of the content included in the Test Classes attachment.


[{"n":"CourseManagerTest"},{"n":"AppleWatchTest"}]

Considerations When Working with Delete Metadata Steps

  1. If the test level selected is RunSpecifiedTests, but the Test Classes attachment doesn't exist in the Delete Metadata step or is incorrect, Copado will apply the default test level.
  2. If the test level selected is NoTestRun and you are deploying to production, Copado will apply the default test level.

As explained above, in either of these two scenarios, Salesforce will select the test level based on the components in the deployment and the environment to which you are deploying.


How did we do?