1

I faced a weird scenario today. I have a @testSetup method for inserting my case records and then used another method to query those case records to fetch CaseNumber field and pass it to a JSON String for Rest API.

When i insert the test records in my @testSetup, the query results for 2 case records fetched the same CaseNumber.

Example:

@isTest
public class someClass_Test {

    @testSetup
    public static void setup() {

        // code for inserting account & custom settings data 

        // inserting Case
        Case newCase1 = new Case(External_System_Reference_Id__c = 'C-002002002002',Origin = 'QAC Website',RecordTypeId = caseRTId,
                                Type = 'Service Request',Sub_Type_1__c = 'General',Sub_Type_2__c = 'Business',Status = 'Open',
                                Subject = 'to be decided',Description = 'some desc',Justification__c = 'must give',Authority_Status__c = 'Approved',
                                AccountId = myAccount.Id,Consultants_Name__c = 'abcd',Consultant_Email__c = 'test@test.com',Consultant_Phone__c = '28213812',
                                PNR_number__c = '4545451',Old_PNR_Number__c = '1021512',Passenger_Name__c = 'TestUser',Regions__c = 'DOM'); 
        insert newCase1;

        Case newCase2 = new Case(External_System_Reference_Id__c = 'C-002002002003',Origin = 'QAC Website',RecordTypeId = caseRTId,
                                Type = 'Service Request',Sub_Type_1__c = 'General',Sub_Type_2__c = 'Business',Status = 'Open',
                                Subject = 'to be decided',Description = 'some desc',Justification__c = 'must give',Authority_Status__c = 'Approved',
                                AccountId = myAccount.Id,Consultants_Name__c = 'abcd',Consultant_Email__c = 'test2@test.com',Consultant_Phone__c = '28213813',
                                PNR_number__c = '4545452',Old_PNR_Number__c = '1021512',Passenger_Name__c = 'TestUser',Regions__c = 'DOM');   
        insert newCase2;
    }

    @isTest
    public static void CaseUpdation()
    {
        Set<String> eachCaseNums = new Set<String>();

        for(Case eachCase : [Select Id,CaseNumber from Case]){
            system.debug('each case no:**'+eachCase.CaseNumber);
            eachCaseNums.add(eachCase.CaseNumber);
        }
        system.debug('case nums**'+eachCaseNums);
    }
}

Debug that I got: In This image, you can see ID is unique but Case number duplicated.

Case number duplicate

ANSWER / UPDATED

I added this ,

list<Case> myCases = new list<Case>();
myCases.add(newCase1);
myCases.add(newCase2);
insert myCases;

I fixed this using list<Case> and inserted it. There i didn't find any duplicates on CaseNumber. But I want to know why is this happening and what difference it had on list<Case> and each record insert.

1

This is a known issue for auto numbers and seems no fix will be provided for this.

As mentioned in this link, salesforce says:

We do not make any assurances on our autonumber scheme in test context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.