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
SubC4i-dev1SubC4i-dev1 

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;
	}

 

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

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

Puja_mfsiPuja_mfsi

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.

             

     

This was selected as the best answer
SubC4i-dev1SubC4i-dev1

Brilliant!  I'm going to implement your solution and afterwards give you a kudos.  Thank you.

Akshay AmbhureAkshay Ambhure

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