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
OnurKOnurK 

100% coverage in Sandbox none in Production

I have a class runs before insert cases. It checks the duplicated tax number. It works perfect in Sandbox but there is covearge problem in Production. Can anyone help with the issue?

 

Thanks

public class TaxNumberDuplicate{

    public static void checkDuplicates (List<Case> caseList) {
    
        List<Case> duplicatecaseList = new List<Case>();
        
        for (Case c : caseList) { 
		
				duplicatecaseList = [SELECT id FROM Case WHERE Tax_Number__c =: c.Tax_Number__c];
			}
            
            if(trigger.isInsert){
            
            
            if(duplicatecaseList.size()> 0 && 
               c.Permit_Duplicate__c== FALSE && 
               c.Tax_Number__c != Null){
            
            c.Tax_Number__c.addError('ERROR');
            }
            }
            else if(trigger.isUpdate){
            
                duplicatecaseList = [SELECT id FROM Case WHERE Tax_Number__c =: c.Tax_Number__c];
            
                if(duplicatecaseList.size()> 0 && 
                   c.Permit_Duplicate__c == FALSE && 
                   c.Tax_Number__c != Null){
            
                c.Tax_Number__c.addError('ERROR');
            
                }
            }

    }
}

 

mroarkmroark

Have you checked the error logs to find out why the code is failing?  At first pass, I would say that it might be possible that you have another trigger or test case which is firing the method in this class for more than 25 Cases, which would cause the trigger to fail due to governor limits.

 

Also, on line 5, you have an extra right curly brace ( '}' ).

 

Your method would handle governor limits better if you looped through the list of passed in Cases, extract the 'Tax_Number__c' for each, then passing this list dynamically to the SOQL using the 'IN' parameter to get a list of Cases with duplicate Tax Numbers.  You can then check if the Case has a sibling with a duplicate Tax Number.  Refactoring the code, it would look like this:

 

public class TaxNumberDuplicate{

    public static void checkDuplicates (List<Case> caseList) {
    
List<String> arrStrCaseTaxNumbers = new List<String>(); List<Case> duplicatecaseList = new List<Case>(); for (Case c : caseList)
{ if(c.Permit_Duplicate__c == FALSE && c.Tax_Number__c != Null)
{ arrStrCaseTaxNumbers.add(c.Tax_Number__c); }
}

duplicatecaseList = [SELECT id, Tax_Number__c FROM Case WHERE Tax_Number__c IN :arrStrCaseTaxNumbers];
for (Case c : caseList)
{ 
if(c.Permit_Duplicate__c == FALSE && c.Tax_Number__c != Null) {
for (Case dupCase : duplicateCaseList)
{
if (dupCase.Tax_Number__c == c.Tax_Number__c)
c.Tax_Number__c.addError('ERROR'); }
}
 } } }

 

Cory CowgillCory Cowgill

In addition to above, is your UnitTest creating the proper records? Or are you relying on existing data in your Unit Tests by doing SOQL? You should make sure your unit tests are creating your test data.

OnurKOnurK

Thanks for your reply, my problem was with the other error codes I guess.  It works fine now. On the  other hand I need to add somewhere following code. Otherwise when I update the code I receive error even though same record accepted as unique. The error message is "Save error: Field expression not allowed for generic SObject"

 

System.Trigger.oldMap.get(c.id).Tax_Number__c != System.Trigger.newMap.get(c.id).Tax_Number__c

 Thanks again for your kind replays.