How to Deactivate/Delete the Apex Class/Triggers in Production

So, Here we have some easy ways to Disable or Delete apex classes/Triggers in production environment for you.

Unlike in sandbox, where you can edit a trigger to uncheck the “Active” checkbox, you cannot do the same in a Production instance.  Its not that straightforward.

Note: A common known issue while deactivating trigger is code coverage (more than 75%).Specially, if the trigger you are turning off has lots of code, it will definitely impact your overall code coverage.  So, be ready with your test classes for the other existing apex classes/triggers.

 

1. DEACTIVATE THE TRIGGER IN SANDBOX AND DEPLOY

This is the simplest way to do of all is Deactivate your trigger in your sandbox by unchecking the Active checkbox of the trigger.  Create an outbound change set, upload and deploy the change set to production.

2. COMMENT YOUR TRIGGER CODE

The other simpler way is comment your entire apex trigger code in your sandbox and then use outbound change set to deploy to production.  To comment code, you need to use /* and */ .  Anything in between those will be commented.

trigger ContactTrigger on Contact (before insert){
/*
your entire code
*/
}

Same for Apex class too.

3. CREATE CUSTOM SETTINGS

Use custom setting with this which you can control whether a trigger should run or not.  Create a custom setting ( Trigger_Settings__c).  Add a checkbox field “Is Active”.  So your custom settings will store the trigger name and its status in IsActive checkbox.

Then in your apex trigger, add an IF condition at the top to check if the Trigger status in Custom settings is TRUE, only then run the entire apex trigger code.

trigger ConTrigger on Contact (before insert){
Trigger_Settings__c TS = Trigger_Settings__c.getValues(‘ConTrigger’);
if(TS.is_Active__c == TRUE){
//your trigger code
}
}

Every time you would like to turn off the trigger in production, You just need to go to your custom settings and uncheck the IsActive checkbox for that trigger.

4. USE ECLIPSE IDE

This is same like change set way but here you need to  make the trigger InActive in the trigger’s XML file and then deploy from Eclipse.

1. First install Force.com IDE
2. Connect to the Sandbox Instance using the IDE and identify the trigger that you want to deactivate.
3. Open the trigger’s .xml file, and change the Status XML tag from Active to Inactive.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
 <apiVersion>31.0</apiVersion>
 <status>Deleted</status>
</ApexClass>

4. Save the file.
5. Select the two files (Code and XML) using “Ctrl-click,” and then right-click on one of them.
6. Select Force.com and Deploy to server.
7. Provide your credentials for the Production org and follow the steps.

TO DELETE: To delete an apex class, change it to Deleted. Apex class Status can only be changed to “Active” or “Deleted,” not “Inactive”.
For more clear understand goto this slide share PPT(Prepared by Agarwal)

 

5. USE ANT

1. Initialise destructiveChanges.xml: First, create an empty destructiveChanges.xml file. Check your destructiveChanges.xml file in to your source code control system.

3. Propagating Destructive Changes to Other Developer Edition Orgs.  Every time you delete a component:

a. Identify the name of the metadata component member.
b. Identify the name of the metadata component type.
c. Add an entry to your destructiveChanges.xml file

4. Repeat steps 3-a,b,c for every component you have deleted.
5. Check in the changes you made to your destructiveChanges.xml file to your source code control system.
6. Note that you should refresh the destructiveChanges.xml files and run the Force.com Migration Tool for Apache Ant against it.

Note:This approach also can be followed to let an apex class be NOT used in production.

6. WORKBENCH

1. To do this using Workbench, create a folder in your PC. example: ‘deleteClasses’.

2. Go to Notepad and copy and paste the below and save as the file with ‘package.xml’ and ‘All files (*.*)’.

 

Note:For all below xml’s change your versions.

<?xml version="1.0" encoding="UTF-8"?>

<Package xmlns="http://soap.sforce.com/2008/04/metadata">

<version>41.0</version>

</Package>

 

3. Then create a new file in Notepadand copy the below into it:

<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://soap.sforce.com/2008/04/metadata">

<types>

<members>class to Delete</members>

<name>ApexClass</name>

</types>

<version>41.0</version>

</Package>

If you have two classes that need to be deleted at the same time, you can simply add another <members> row into the file with the name of the other class, for example:

<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://soap.sforce.com/2008/04/metadata">

<types>

<members>class1 to delete</members>

<members>class2 to delete</members>

<name>ApexClass</name>

</types>

<version>41.0</version>

</Package>

4. Save the file with name as destructiveChanges.xml (note the capital ‘C’ in ‘changes’). Make sure the file is saved as ‘All files (*.*).

 

5. Open the folder, select both the XML files, right-click and make it compression  ‘Send To > Compressed Folder’. Keep the default name of ‘package’ for the folder .

6. You are now setup to deploy the destructiveChanges.xml file to Salesforce. To do this, go to Workbench and login with your credentials.

7.  Select Migration > Deploy.

8. Click ‘Browse’ and select the .zip package file.

Then check ‘Rollback on Error’, ‘Single Package’, and select Test Level with ‘RunLocalTests’.

9. Now, select ‘Next’ and then you will notice that the success or error results will be displayed in Workbench once the deployment process has been completed.

 

Knowledge Articles:

https://help.salesforce.com/articleView?id=000003942&type=1

https://help.salesforce.com/articleView?id=000006188&type=1

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s