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
Nithya S 1Nithya S 1 

Recursion check is causing a code coverage error.

Hi all,
There is a trigger on the lead that is cauing a APEX CPU time out which we found was due to the recursion. Added the code to ensure that recursion doesn't occur, but that is now causing a code coverage error when deploying to production.
 
Setting the value to "false" before the insert in the test class is resulting in more than 80% coverage in sandbox (when the test class is run individually). But when the validation occurs in production, it causes a code coverage error.

Please assist.


Trigger
trigger UpdateNewOpportunity on Lead (After Update, before Insert, before update) {
 if(!Acc_RecursionCheck.LeadRecursion){
 Acc_RecursionCheck.LeadRecursion = true;
    if(trigger.Isupdate && trigger.IsAfter){
        Map<ID,Lead> leadMap = new Map<Id, Lead>();
....
....


Test Class:
private class ClassForLeadtest {
        static testMethod void UpdateNewOpportunityTest() {
        
        try
        {
        system.test.startTest();
        List<Lead> leadList = new List<Lead>();
        Lead leadObj = new Lead( FirstName = 'Test1', LastName = 'Lead', Phone = '1234567890', Email = 'test1@lead.com', Company = 'Test Company'');
          Acc_RecursionCheck.LeadRecursion = false;
          insert leadObj;
          ...
          ...
          ..
          }
          }
Dilip_VDilip_V
Nithya,

Acc_RecursionCheck.LeadRecursion is static variable ?
if not its really difficult to manage the recursions using local variables in static method.Use static varibles to controle the flow.

Try this solution once.

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Find out if there are any other triggers in production that causing this error.

Thanks.
Nithya S 1Nithya S 1
Hi Dilip, Yes, the variable is a global static variable. Thanks!