You need to sign in to do that
Don't have an account?
Write Apex Test Class for Trigger with Recursive Control
I have a trigger with recursive control that performs both Before Insert and Before Update, but I cannot figure out how to test the Before Update in my test class. When I insert the test data in my unit test, the recursive variable is set to true and will block my update from setting off the trigger. This is only desirable for when it runs in production, but not when I need to insert data in order to test situations where existing data is being updated. I even tried inserting the test data above the test.startTest() line. But this does not matter since every action in the test method occurs in the same context.
How do I write my test class to cover this?
Trigger code:
trigger PriceModifierTrigger on OpportunityLineItem (before insert, before update) { if(!PriceModifierClass.hasAlreadyRunMethod()){ if(trigger.isBefore && trigger.isInsert){ PriceModifierClass.sortOppProducts(trigger.new, trigger.oldMap, 'Insert'); } if(trigger.isBefore && trigger.isUpdate){ PriceModifierClass.sortOppProducts(trigger.new, trigger.oldMap, 'Update'); } PriceModifierClass.setAlreadyRunMethod(); } }
Excerpt from class:
public static boolean hasAlreadyRun = false; public static boolean hasAlreadyRunMethod(){ return hasAlreadyRun; } public static void setAlreadyRunMethod(){ hasAlreadyRun = true; }
Hi,
You need to reset the recursive variable in test class:
@isTest
private class TestPriceModifierTrigger {
static testMethod void unitTest() {
// Your code
OpportunityLineItem opptyLine = new OpportunityLineItem (
//put your values
);
insert opptyLine;
// now you need to reset your static variable here
className.hasAlreadyRun = false;
//Then update the oppotunity line item
update opptyLine;
)
)
Please let me know if u have any problem on same and if this post helps u please throws KUDOS by click on star at left.
All Answers
Hi,
You need to reset the recursive variable in test class:
@isTest
private class TestPriceModifierTrigger {
static testMethod void unitTest() {
// Your code
OpportunityLineItem opptyLine = new OpportunityLineItem (
//put your values
);
insert opptyLine;
// now you need to reset your static variable here
className.hasAlreadyRun = false;
//Then update the oppotunity line item
update opptyLine;
)
)
Please let me know if u have any problem on same and if this post helps u please throws KUDOS by click on star at left.
Brilliant! I'm going to implement your solution and afterwards give you a kudos. Thank you.
Hi @Puja_mfsi
woooow Fantastic. Thank you so much
It worked for me also
I littlrly spent my whole DAY any going again and again DEBUG log and now its just worked
I wanted to let you know i came to this article from this below article, I was not aware about if we have Trigger AFTER UPDATE and Recursive Trigger Handler class then how to get Test coverage
https://salesforce.stackexchange.com/questions/308418/after-update-test-class-is-not-committing-change