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
bstangerbstanger 

Temporarily "disable" trigger that runs when another trigger runs

I have a trigger that mass updates some custom fields on OpportunityLineitems.

 

I have a trigger on OpportunityLineitems that updates the same custom fields when an OpportunityLineitem is updated.

 

The first trigger causes the 2nd trigger to run.

 

How do I stop the 2nd trigger from running temporarily?

 

Any help would be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

You'll need to use some static variables to do the trick, examples can be found on these two blogs:

 

http://www.tehnrd.com/apex-flow-control-preventing-infinite-loops/

http://blog.jeffdouglas.com/2009/10/02/preventing-recursive-future-method-calls-in-salesforce/

 

Both examples include @future but contain the gist of what you need.

 

Cheers,

Wes

All Answers

WesNolte__cWesNolte__c

Hey

 

You'll need to use some static variables to do the trick, examples can be found on these two blogs:

 

http://www.tehnrd.com/apex-flow-control-preventing-infinite-loops/

http://blog.jeffdouglas.com/2009/10/02/preventing-recursive-future-method-calls-in-salesforce/

 

Both examples include @future but contain the gist of what you need.

 

Cheers,

Wes

This was selected as the best answer
bstangerbstanger

I used the example from http://www.tehnrd.com/apex-flow-control-preventing-infinite-loops/

 

 

I created the following class:

 

public class executionFlowUtility {
     public static boolean doNotUpdate;
}

 

 

Here's the trigger set hedoNotUpdate variable to true

 

trigger UpdateTeacherStdCostAfterStndCostInsertUpdate on Teacher_Standard_Costs__c (after insert, after update) {
Teacher_Standard_Costs__c [] TSC = Trigger.new;

List<CurrencyType> myCurrency = [Select c.Id, c.IsoCode, c.ConversionRate From CurrencyType c];
For (CurrencyType curr: myCurrency)

{
    List<OpportunityLineItem> oli = [Select Id from OpportunityLineItem
    WHERE CostDate__c = :TSC[0].Month__c or CostDate__c = null];
   
        for (OpportunityLineItem ol: oli)
        {    ol.CostDate__c = TSC[0].Month__c;
            ol.Teacher_Var_Cost_Per_Hr__c = TSC[0].Variable_Cost_Per_Hour__c * curr.ConversionRate;
            ol.Teacher_CGS_Per_Hr__c = TSC[0].CGS_Per_Hour__c * curr.ConversionRate;
            ol.Teacher_G_A_Cost_Per_Hr__c = TSC[0].G_A_Cost_Per_Hour__c * curr.ConversionRate;
        }
        executionFlowUtility.doNotUpdate = True;
        update oli;
}

}

 

 

Here's the first few lines of the trigger I do not want to run when the previous trigger runs:

 

trigger GetCurrentTeacherCost on OpportunityLineItem (before insert, before update) {
system.debug(executionFlowUtility.doNotUpdate);
if (executionFlowUtility.doNotUpdate == null ||executionFlowUtility.doNotUpdate == false)
 
{

 

The system log tells me that doNotUpdate= true, yet the above trigger continues to run.

 

Any idea why?

WesNolte__cWesNolte__c

Hmm.. we might need tehnrd here, but let's try a few things before giving him a nudge. Firstly could you try changing this,

 

public static boolean doNotUpdate;

 

to this,

 

public static boolean doNotUpdate{get;set;}

 

I've had some strange issues when not explicitly makeing public vars properties or getters/setters. If that doesn't work change your condition,

 

 if (executionFlowUtility.doNotUpdate == null ||executionFlowUtility.doNotUpdate == false)

 

to

 

if (executionFlowUtility.doNotUpdate <> true)

 

These seem like silly things to do, but when you've tried the obvious you need to question the not so obvious.

 

Wes

bstangerbstanger

Thanks Wes.  It was working before, I just assumed that it wasn't because the debug log (pertinent part below) referenced the trigger I was trying to stop from running and the DML rows "Close to Limit" message in that section. 

 

I assumed that when I skipped the trigger, I would not get issues with DML rows in that trigger.  I'm not sure how to get around this.  Any thoughts?

 

 

 

*** Beginning GetCurrentTeacherCost on OpportunityLineItem trigger event BeforeUpdate for 00k8000000Ct5BD, 00k8000000Ct5BE, 00k8000000Ct5Bm, 00k8000000Ct5Bn, 00k8000000Ct5Rk, 00k8000000DIKRm, 00k8000000DIKS5, 00k8000000DIKSF, 00k8000000DIKTi, 00k8000000DIKTj, 00k8000000DIKTk, 00k8000000DIKaK, 00k8000000DIKaL, 00k8000000DIKaM, 00k8000000DIKaN, 00k8000000DIKcS, 00k8000000DIKcx, 00k8000000DIKcy, 00k8000000DIKcz, 00k8000000DIKfV, 00k8000000DIKfW, 00k8000000DIKfX, 00k8000000DIKfY, 00k8000000DIL1d, 00k8000000DIL1e, 00k8000000DIL1f, 00k8000000DIL5A, 00k8000000DIL5B, 00k8000000DIL5C, 00k8000000DIL5F, 00k8000000DIL5d, 00k8000000DIL5e, 00k8000000DIL5f, 00k8000000DIL5g, 00k8000000DIL9B, 00k8000000DIL9D, 00k8000000DIL9E, 00k8000000DIPIu, 00k8000000DIPIw, 00k8000000DIPKq, 00k8000000DIPLF, 00k8000000DIPMh, 00k8000000DIPMi, 00k8000000DIPN1, 00k8000000DIPVQ, 00k8000000DIPVR, 00k8000000DIPVS, 00k8000000DIPac, 00k8000000DIPad, 00k8000000DIPae, 00k8000000DIPah, 00k8000000DIPb1, 00k8000000DIPbH, 00k8000000DIPbI, 00k8000000DIPbJ, 00k8000000DIV1O, 00k8000000DIV1P, 00k8000000DIV1Q, 00k8000000DIV1s, 00k8000000DIV1t, 00k8000000DIV2H, 00k8000000DIV2I, 00k8000000DIXWs, 00k8000000DIdwB, 00k8000000DIdwC, 00k8000000DIdwD, 00k8000000DIe0B, 00k8000000DIe0p, 00k8000000DIe0u, 00k8000000DIe2W, 00k8000000DIe2X, 00k8000000DIeEW, 00k8000000DIeEb, 00k8000000DIeEc, 00k8000000DIeEd, 00k8000000DIeEe, 00k8000000DIeEf, 00k8000000DIeEq 20091020141157.120:Trigger.GetCurrentTeacherCost: line 2, column 1: This is it 1 true Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 2 out of 20 Number of query rows: 81 out of 1000 Number of SOSL queries: 0 out of 0 Number of DML statements: 1 out of 20 Number of DML rows: 78 out of 100 ******* CLOSE TO LIMIT Number of script statements: 555 out of 10200 Maximum heap size: 0 out of 200000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 0 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Static variables and sizes: executionFlowUtility:doNotUpdate:4 *** Ending GetCurrentTeacherCost on OpportunityLineItem trigger event BeforeUpdate for 00k8000000Ct5BD, 00k8000000Ct5BE, 00k8000000Ct5Bm, 00k8000000Ct5Bn, 00k8000000Ct5Rk, 00k8000000DIKRm, 00k8000000DIKS5, 00k8000000DIKSF, 00k8000000DIKTi, 00k8000000DIKTj, 00k8000000DIKTk, 00k8000000DIKaK, 00k8000000DIKaL, 00k8000000DIKaM, 00k8000000DIKaN, 00k8000000DIKcS, 00k8000000DIKcx, 00k8000000DIKcy, 00k8000000DIKcz, 00k8000000DIKfV, 00k8000000DIKfW, 00k8000000DIKfX, 00k8000000DIKfY, 00k8000000DIL1d, 00k8000000DIL1e, 00k8000000DIL1f, 00k8000000DIL5A, 00k8000000DIL5B, 00k8000000DIL5C, 00k8000000DIL5F, 00k8000000DIL5d, 00k8000000DIL5e, 00k8000000DIL5f, 00k8000000DIL5g, 00k8000000DIL9B, 00k8000000DIL9D, 00k8000000DIL9E, 00k8000000DIPIu, 00k8000000DIPIw, 00k8000000DIPKq, 00k8000000DIPLF, 00k8000000DIPMh, 00k8000000DIPMi, 00k8000000DIPN1, 00k8000000DIPVQ, 00k8000000DIPVR, 00k8000000DIPVS, 00k8000000DIPac, 00k8000000DIPad, 00k8000000DIPae, 00k8000000DIPah, 00k8000000DIPb1, 00k8000000DIPbH, 00k8000000DIPbI, 00k8000000DIPbJ, 00k8000000DIV1O, 00k8000000DIV1P, 00k8000000DIV1Q, 00k8000000DIV1s, 00k8000000DIV1t, 00k8000000DIV2H, 00k8000000DIV2I, 00k8000000DIXWs, 00k8000000DIdwB, 00k8000000DIdwC, 00k8000000DIdwD, 00k8000000DIe0B, 00k8000000DIe0p, 00k8000000DIe0u, 00k8000000DIe2W, 00k8000000DIe2X, 00k8000000DIeEW, 00k8000000DIeEb, 00k8000000DIeEc, 00k8000000DIeEd, 00k8000000DIeEe, 00k8000000DIeEf, 00k8000000DIeEq 

WesNolte__cWesNolte__c

Glad it worked:)

 

If I were to hazard a guess I'd say that you're seeing that output because the trigger is firing, all you're doing is channel the logical flow.. so you're not really skipping the trigger.

 

I can see how this might end up being an issue, but I think we'd have to chat with one of the guys working under the hood in order to find out what we could do(if anything).

 

Good luck bud.

Wes