2

I have a static boolean in the handler class for the trigger that will prevent a subsequent call from the same context but how do I test this? Apex tests serialize and run an operation in order. I have tried:

handlerClass.alreadyProcessed = True // static variable
update recordList; // list that I want to fail update as a result of recursion
  • Apologies if I missed a question already asked somewhere on this. I didn't see one. – Andrew Mayes May 19 '17 at 23:16
1

If recordList update causes that trigger to fire, query for the records after you do the update call and assert that whatever the trigger was supposed to do did not happen.

Suppose I have a trigger on Account

trigger myExample on Account(before update){

     if(handlerClass.alreadyProcessed == true) return;

     for(Account a : trigger.new)
        a.Name = 'I updated myself');

}

Then in my test I would

Account a = New Account(Name = 'Test');
insert a;
handlerClass.alreadyProcessed = true;
update a;

a = [Select Name From Account Where Id = :a.Id];
system.assertEquals('Test',a.Name,'The Name was updated when the recursion check was set to true');

//May be more appropriate to explicitly check for the potential updated value in case there are WFR or something else changing the value. This would check explicitly that the trigger value was not set
system.assertNotEquals('I updated myself',a.Name,'The Name was updated when the recursion check was set to true');
  • I see. One more question. It should be a static variable for alreadyProcessed correct? – Andrew Mayes May 20 '17 at 5:16
  • @AndrewMayes yes. Keep in mind though if processing more than 200 records it does not get reset between the chunks IIRC – Eric May 20 '17 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.