function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Dev in TrainingSalesforce Dev in Training 

Disabling a Trigger

Hey SF Dev Community,

I created a trigger that has one error in it. I need to disable it as I've already created a new change set with the fix, what's the best method to do this?

Thank you

Best Answer chosen by Salesforce Dev in Training
Temoc MunozTemoc Munoz
I see.

If you replace the code in Sandbox and push to Production, the code in Production will be replaced by your change.
Always try to backup your code before pushing to production (i.e. source control, etc).

Thanks

All Answers

Temoc MunozTemoc Munoz
Hi.

You can either comment out the trigger code or you can set a flag using a custom label:
 
trigger on YourObject__c(......)
{
    if(Label.triggerIsEnabled)
    {
        // your logic here
    }
}

Thanks
Salesforce Dev in TrainingSalesforce Dev in Training
Well, I just want to remove "before update" from the code. I don't know how to replace the existing code with the new code? I created a change set and pushed it to Production, but won't both triggers exist?
Temoc MunozTemoc Munoz
I see.

If you replace the code in Sandbox and push to Production, the code in Production will be replaced by your change.
Always try to backup your code before pushing to production (i.e. source control, etc).

Thanks
This was selected as the best answer
Salesforce Dev in TrainingSalesforce Dev in Training
Thank you Temoc.
Temoc MunozTemoc Munoz
You got it man!
Salesforce Dev in TrainingSalesforce Dev in Training

Quick question...

Do you know how to add "does not contain" to an Apex trigger?

For example, I have the following code:

trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
        if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
            ta.Subject += ' ('+ta.Task_Results__c + ')';
        }
    }
}

What's happening is every time a user "Edits" and "Saves" a Task, it's duplicating the Task Result value in the Subject line. I only want this to happen once. Is there a way to add "and ta.Subject DOES NOT CONTAIN ta.Task_Results_c" or ONLY UPDATE ONCE?