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
iswarya sekar 7iswarya sekar 7 

Hii Everyone!! I have to deactivate an trigger which is currently running without deleting it. Anyone help me to solve this.

trigger UpdateStage on Account (after insert, after update)    
    List<Id> accountId = new List<Id>();
    
    for(Account acc : Trigger.new)
    {
        if(acc.All_Opportunities_Won__c==True)      
         accountId.add(acc.Id);
    }
    
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp : [select id, StageName, Amount from Opportunity where AccountId in: accountId AND Amount != 0]) 
    {
        opp.StageName='Closed-Won';          
 oppsToUpdate.add(opp); 
    }
    
    update oppsToUpdate;  
 }

This is my apex Trigger. how to deactivate this?
Best Answer chosen by iswarya sekar 7
Dev_AryaDev_Arya
Hi iswarya,

you can simple mark the trigger inactive, please refer the screen shot attached.

User-added image
In case it is in production, you need to follow the proper channel as marking the trigger inactive in sandbox/Developer org and migrate it to production using changesets. This link provides more insight
https://help.salesforce.com/articleView?id=000005417&type=1

If you still have doubts, feel free to ask else if this helps, please mark the solution as green.
Cheers, Dev

All Answers

Arpit Jain7Arpit Jain7
Hello Iswarya,

You can refer below discussion related to same thread

https://developer.salesforce.com/forums/?id=906F0000000MJM9IAO

Thanks
Arpit
 
Dev_AryaDev_Arya
Hi iswarya,

you can simple mark the trigger inactive, please refer the screen shot attached.

User-added image
In case it is in production, you need to follow the proper channel as marking the trigger inactive in sandbox/Developer org and migrate it to production using changesets. This link provides more insight
https://help.salesforce.com/articleView?id=000005417&type=1

If you still have doubts, feel free to ask else if this helps, please mark the solution as green.
Cheers, Dev
This was selected as the best answer
iswarya sekar 7iswarya sekar 7
Thank you Dev, and also I want to know how to use Custom Settings to Deactivate this? could you help me?
 
Dev_AryaDev_Arya
Yes you can, but that also includes modifying your trigger a bit. 
1. Creating the custom setting,
2. Modify your trigger to check the custom setting value
           Set your logic based on the custom setting value. For example: if your custom setting MyCustomSettingsObject.TriggerStatus__c' value is set, and then, execute the trigger logic  else skip everything.
if(MyCustomSettingsObject.TriggerStatus__c== true){ <your code>};

If you still have doubts, feel free to ask else please mark the solution as green.
Cheers, Dev
iswarya sekar 7iswarya sekar 7
Hi Dev,
I dont know how to use this!!
iswarya sekar 7iswarya sekar 7
Is the Same function can be done using apex class??
Dev_AryaDev_Arya
Well, Trigger is executed only after apex code is executed. One cannot control the Trigger execution from Apex, however you can control the behaviour of the trigger. For example: if there is particular field in the modified or inserted Object, the field which you can set in Apex and check in the trigger like Trigger.new or Trigger.old, then you can control the execution statements with in the Trigger. 
 
iswarya sekar 7iswarya sekar 7
I dint use Apex class
Dev_AryaDev_Arya
Sorry, but now I am kinda lost. Could you explain what you are actually seeking for? Custome setting, apex class or something else?
Arpit Jain7Arpit Jain7
Hi Iswarya,

Please see below sample code. May be this could help you to understand the complete flow related to trigger, class, custom setting.

//Your Trigger Code
trigger UpdateStage on Account (after insert, after update){
      Suppose you are having one custom setting then below will be the code to on/off your trigger logic
      TriggerSupport__c support = TriggerSupport__c.getAll();
      Boolean isOff = support.get('Account').TriggerOff__c;
      if(! isOff) // Trigger will only execute when TriggerOff__c field is not checked in custom setting 'Account' record.
      {
          UpdateStageHelper.AfterInsert(trigger.New);
      }
}
// Your trigger handler class
public class UpdateStageHelper(){
     public static void AfterInsert(list<Account> triggerNew){
            // Your Logic...
     }
}

Let me know for any further question or concens.

Please let us know if this helps !!
Thanks
Arpit
iswarya sekar 7iswarya sekar 7
Thank you Arpit !! Now My requirement is can we create a apex class to update child from parent object?