0

I am writing test class for the below method. But it is giving me error - List has no rows for assignment to SObject

get{
            if(PCAppSwitch__c.getInstance().Candidate_Record_Type_API_Name__c == null || PCAppSwitch__c.getInstance().Candidate_Record_Type_API_Name__c == ''){
                PCAppSwitch__c pcAppCustomSetting = PCAppSwitch__c.getOrgDefaults();
                pcAppCustomSetting.Candidate_Record_Type_API_Name__c = 'Independent_Candidate';
                upsert pcAppCustomSetting; 
            }
            return [Select Id, Name From RecordType where DeveloperName =: PCAppSwitch__c.getInstance().Candidate_Record_Type_API_Name__c or DeveloperName = 'Candidate_Independent'];
        }

I tried to insert a new custom setting in test method but it's not working.

PCAppSwitch__c pcAppCustomSetting = new PCAppSwitch__c();
    pcAppCustomSetting.Name = 'Test Setting';
    pcAppCustomSetting.Candidate_Record_Type_API_Name__c = 'Whatever';
    insert pcAppCustomSetting;

Could you tell me what i am doing wrong?

1
  • N.B. Use String.isBlank(PCAppSwitch__c.getInstance().Candidate_Record_Type_API_Name__c) to make if condition simpler
    – cropredy
    Sep 10, 2022 at 0:34

1 Answer 1

1

It seems field Candidates Record type should be null.

Try this :

PCAppSwitch__c pcAppCustomSetting = new PCAppSwitch__c();
    pcAppCustomSetting.Name = 'Test Setting';
    pcAppCustomSetting.Candidate_Record_Type_API_Name__c = ''; //marked this as null
    insert pcAppCustomSetting;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .