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
SanchSanch 

Apex question

Hi guys,
 
I have a bunch of entities, and all of them have trigger's for their own before and after triggers.  The top parent entity trigger triger's bunch of other entities trigger because it updates some fields in those entities.  Due to this, I run out of the Query limit.  The problem is, sometimes, I don't need the trigger to do anything, but the trigger still get trigered.  I can't seem to control how to stop the trigger from triggering with in the code.  Is this possible? If yes, how can I do this? Thanks.
 
Sanch.
TehNrdTehNrd
Im not sure if you can stop triggers from running but if statements are your friend. You say sometimes a trigger doesn't need to be run, therefore there must be a condition for this. Just enclose the trigger in a if statement.

if(something == true){

Execute the trigger code in here

}

Even with this type of control statement you still need to ask yourself will there ever be a situation in which all the riggers would still get fired. If so you'll still have the same problem. Also, are your triggers setup to process the records in bulk? If not, you will hit the governing limits real quick.
TehNrdTehNrd
You can also do if statements based on what type of trigger it is. Not sure if this will help but here is an example

if(Trigger.isDelete){

Do something.

}


SanchSanch
Hi TehNrd,
 
Yes I am processing them in bulk.  I am not sure if I did not explain this properly or if I understand you corrently.  How can I use an if statement within the trigger to check if another trigger need's to be executed or not.  That is my question.  I have
 
Trigger A - 2 triggers, one for before and one for after
Trigger B - 2 triggers, one for before and one for after
Trigger C - 1 for before
Trigger D - 1 for before
 
Trigger A get's updated
 
 
 
SanchSanch
Hi TehNrd,
 
Yes I am processing them in bulk.  I am not sure if I did not explain this properly or if I understand you corrently.  How can I use an if statement within the trigger to check if another trigger need's to be executed or not.  That is my question.  I have
 
Trigger A - 2 triggers, one for before and one for after
Trigger B - 2 triggers, one for before and one for after
Trigger C - 1 for before
Trigger D - 1 for before
 
Trigger A get's updated
 
 
 
TehNrdTehNrd
Which query limit are you hitting? 20 SOQL queries or the amount of records returned with a SOQL query.

Could you provide some more information on the business process and what you are trying to accomplish.
SanchSanch
oops, I guess I pressed something too quickly and it posted the message twice. :)
 
Well, here is the set of events:
 
- Trigger A get's triggered
- The Before and After trigger of A get's triggered
- With in the After trigger of A, it updates the entity B
- Which in turn run Trigger B
- It executes before and after trigger
- in the after trigger of Trigger B, it updates entity C
- this trigger's trigger C.
- After all this is done, now within the Trigger A's after trigger, I am updating the entity D. In this case, I dont' want the trigger for this entity to run. 
 
Is this possible?
SanchSanch
I am hitting the 20 SOQL query limit. Please look at the last post for what i am actually trying to archieve.
TehNrdTehNrd
I now see what your problem is (I misinterpreted your original post) and I don't know of any sure fire fixes but it might be helpful if there was a little more detail about the business process because it looks as if you may need to restructure your code.

Are any of those triggers related in some sort of way or do they all address unique issues. If they are related perhaps you could consolidate triggers and use 'update' to process the related records rather than a trigger on each object.

Sorry I couldn't be of more help.

Message Edited by TehNrd on 08-20-2007 01:37 PM

vertdevvertdev
It may be possible for you to utilize an APEX class to perform the behavior that you are looking for.  When trigger A fires youcould set  a variable in the the APEX class.  Then in any of the other transactions you could check to see if that value is set in the class.  If the value is set then you could have it skip the action.

I currently use APEX classes in this way to avoid recursion when dealing with triggers that have the potential to create an infinite loop. 


SanchSanch
Thank you guys for the suggestions.  vertdev suggestion is pretty much the only way to go about this.  Thanks guys.
TehNrdTehNrd
While I haven't run into a infinite loop or too many SOQL queries problem it seems like a best practice to incorporate some sort of flow control after reading this thread. As we add more code it will be more likely that we will encounter one of these problems and it would be easier to create it now rather than later.

I created a flowControl class with a variable and two methods. One that returns the variable and another that sets the variable. Is it possible to set the variable in the class directly without having to use a method?

Class:
Code:
global class flowControl {
  
  //---------------------------Application Automation Trigger Control--------------------------
  public static boolean runApplicationAutomation = true;
  
  //Return the run trigger boolean   
  public static boolean runApplicationAutomation(){
   return runApplicationAutomation;
  }
  
  //Set the run Trigger boolean
  public static void setRunApplicationAutomation(boolean value){
   if(value == true){
    runApplicationAutomation = true;
   }else{
    runApplicationAutomation = false; 
   }
  }
}
Its use  in a Trigger:
Code:
trigger applicationAutomationOnOpportunity on Opportunity bulk (before insert, before update) {
 
     boolean runTrigger = flowControl.runApplicationAutomation();
     if(runTrigger == true){
      Trigger Code
     }
}

 

Message Edited by TehNrd on 08-22-2007 04:12 PM

Message Edited by TehNrd on 08-23-2007 10:13 AM