You need to sign in to do that
Don't have an account?

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'); } } } }
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:
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.
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"
Thanks again for your kind replays.