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
Aidel BruckAidel Bruck 

code coverage is 96% in sandbox and 37% in production and production code coverage is 88%

I have a trigger and and a test class. 
In the sandbox the code coverage is 96% 
But in production only 37%
The thing is, Total coverage in production is 88%, so I  have no idea what the issue could be.
This is the trigger and test class

trigger DoubleAccountName on Account (after insert, after update) 
{

    decimal i=0;
    list<account> accountstoupdate= new list<account>();
    account accounttoupdate= new account();
    set<string> accountnames= new set<string>();
    set<id> accountids= new set<id>();
    system.debug('new trigger '+trigger.new);
        for(account a: trigger.new)
        {
            if(a.FirstName== '')
                accountnames.add(a.LastName);
            else
                accountnames.add(a.firstname+' '+a.lastname);
            accountids.add(a.id);
        }
            
    //system.debug(accountnames);
    //system.debug(accountids);
        

    list<account> allaccountswithname= [select original_last_name__C, firstname, lastname, name, namecount__C from account where original_last_name__c in: accountnames and id not in: accountids];
    //stem.debug('all accounts with names .'+allaccountswithname);
    
    if(trigger.isinsert)
    {
       
            if(!allaccountswithname.isEmpty())
            {
                
                for(account a: trigger.new)
                {
                    for(account matchingaccount: allaccountswithname)
                    {
                        //stem.debug('a in loop '+ a);
                        i=0;
                        if(a.FirstName+' '+a.LastName== matchingaccount.Original_last_name__c)
                            if(i<matchingaccount.namecount__C)
                                i= matchingaccount.namecount__C;
                    }  
                        
                        accounttoupdate.id = a.id;
                        //stem.debug('account name insert '+ accounttoupdate.LastName);
                        accounttoupdate.namecount__c= i+1;
                        accounttoupdate.FirstName= a.FirstName;
                        accounttoupdate.Lastname=a.lastname+(i+1);
                        accounttoupdate.Check_for_account_duplicate__c= true;    
                        accounttoupdate.Original_last_name__c= a.FirstName+' '+ a.Last_Name__c;
                        accountstoupdate.add(accounttoupdate);
                }
                
                
                
             if(!accountstoupdate.isEmpty())
                update accountstoupdate;   
                
            }
             
           
        }
        
    
        
    
    
    
    if(trigger.isupdate)
    {
        for(account a: trigger.new)
            for(account aold: trigger.old)
            if((a.firstname!= aold.firstname || a.lastname!= aold.lastname )&& aold.Check_for_account_duplicate__c== false)
            {
                
                if(!allaccountswithname.isEmpty())
                {
                    for(account matchingaccount: allaccountswithname)
                    {
                        i=0;
                        if(a.FirstName+' '+a.LastName== matchingaccount.Original_last_name__c)
                            if(i<matchingaccount.namecount__C)
                                i= matchingaccount.namecount__C;
                    }
                        //stem.debug('update last name '+accounttoupdate.Lastname);
                        accounttoupdate.id = a.id;
                        accounttoupdate.namecount__c= i+1;
                        accounttoupdate.FirstName= a.FirstName;
                        accounttoupdate.Lastname=a.LastName+(i+1);
                        accounttoupdate.Check_for_account_duplicate__c= true;    
                        accounttoupdate.Original_last_name__c= a.FirstName+' '+ a.Last_Name__c;
                        accountstoupdate.add(accounttoupdate);
                }
            } 
                
                
             if(!accountstoupdate.isEmpty())
             {
                 update accountstoupdate;  
                 //stem.debug('accountstoupdate '+accountstoupdate);
             }
                 
        }
                
       
    


}


@isTest
public class TestDoubleAccountName 
{
    public static testmethod void MyUnitTest()
    {
        
       test.startTest();
        
        account a= testdatafactory.createPersonAccount('test');
        insert a;
        
        print('firsta', a);
        
        account a2= testdatafactory.createpersonaccount('test');
        insert a2;
        print('seconda', a2);
        
        a.LastName= 'patient1';
        a.Check_for_account_duplicate__c= false;
        update a;
        print('update a', a);
        
        account a3= testdatafactory.createpersonaccount('alligator');
        a3.LastName= 'bumbelbie';
        insert a3;
        
        a3.FirstName= 'test';
        a3.Lastname= 'patient';
        update a3;
            
        print('update a3', a3);
      
        test.stopTest();
        
    }
    
    public static void print(string message, account a)
    {
        list<account> atoprint= [select id, name, Check_for_account_duplicate__c, namecount__C from account where id=: a.id];
        system.Debug(message);
        system.debug(atoprint);
    }

}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Aidel,

Greetings to you!

Please check that all test classes are creating their own test data and do not rely on org's data. 

You sandbox and production instances have totally different data, so your tests will run on different data as well.

The way around this is to create all new records at the beginning of your test class. Make sure not to use the SeeAllData=true annotation!

Also, your production org might have certain configurations that may be lowering its coverage. These could be validation rules, custom settings, workflows, etc.  

Please refer to the below links which might help you further with the above requirement.

https://help.salesforce.com/articleView?id=000213655&language=en_US&type=1

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas