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
1
  • Apologies if I missed a question already asked somewhere on this. I didn't see one. May 19, 2017 at 23:16

1 Answer 1

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');
2
  • I see. One more question. It should be a static variable for alreadyProcessed correct? May 20, 2017 at 5:16
  • 1
    @AndrewMayes yes. Keep in mind though if processing more than 200 records it does not get reset between the chunks IIRC
    – Eric
    May 20, 2017 at 5:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .