I recently had to disable a Salesforce Trigger from a client’s production environment. Having discovered the need last minute for a project, I needed to react quickly and make the change quickly. Since the official documentation is a bit lacking I decided it was time for blog post of better directions.
The Salesforce CLI directions in the article above make a few annoying assumptions:
- That you’re happy to use MDAPI not SFDX.
- That all your tests pass in production (which should be true, but let’s be real it;s always).
- You enjoy working out CLI commands in a rush.
What you need
Disabling a Trigger Using SFDX in VS Code.
Connect VS Code to your target org. Easiest solution here is to just go to the command palate and authorize an org.
Pull down the trigger from your org. I find it easiest to go to the metadata browser, find the trigger under Apex Triggers and click the download icon on the right.
Update the trigger’s XML file to change the status. All project code files in SFDX are accompanied by an XML file of class metadata. Open the file and change the status to “Inactive”.
Generate a Manifest file for your trigger. Right click on the trigger code file in VS Code’s file explorer and select Generate Manifest, provide the file a useful name (in this case used DisableTrigger)
Deploy the change. If all your org’s tests currently pass you can just right click on the new manifest file and instruct VS Code to deploy the source in the manifest to the org.
What if tests fail on trigger deploy?
While all our tests should always pass all the time, Salesforce admins frequently find that’s not actually true in practice. Heck this trigger could be part of that problem in your org. But to deploy a code change we have to run some tests. Since we are disabling this trigger, the trigger code doesn’t need to be in tests we just need to run a working test with enough coverage to get the job done. So go find a test class and then we can deploy.
VS Code doesn’t currently have a setting to set the testing mode on a deployment, so you’ll need to do this last step in VS Code’s terminal (available by hitting control-` if you don’t have it open already). In the terminal run the following command (replacing [good_tests] with the name of your test class).
sfdx force:source:deploy -x manifest/DisableTrigger.xml -l RunSpecifiedTests -R [good_tests]
It should deploy pretty quickly, but you can check the status by going to settings in your org and checking the Deployment Status.