[SalesForce] Custom Setting GetInstance method returns null

I am currently having an issue with my custom settings returning null values when they should, to the best of my knowledge, return a value.

The following code in Anonymous Apex:

System.Debug(LoggingLevel.Info, [SELECT Name, Id FROM Country__c WHERE Name = 'United States of America']);
System.Debug(LoggingLevel.Info, Country__c.getValues('United States of America'));    

Clarification on the Debug Statements

I used SOQL within my debug statement to query for the custom setting the first time to only verify that:

  • I actually had a custom setting of that name.
  • It could be searched by the Name field – just like a GetInstance or GetValues methods would.

The results I got back clarify that searching by the name value within SQOL will get the correct custom setting information. However, my GetInstance and GetValues method returns null.

Both return different results (when they shouldn't):

14:25:24.053 (53689000)|USER_DEBUG|[1]|INFO|(Country__c:{Name=United States of America, Id=a01a000000anByPAAU})
14:25:24.054 (54989000)|USER_DEBUG|[2]|INFO|null

Am I missing something? I was always under the impression that both should work the same.

Other Information

I used the SOQL statement to verify that the custom setting exists and can be queried for by the Name – what you use to retrieve instances of a custom setting.

I have even tried using GetInstance:

System.Debug(LoggingLevel.Info, Country__c.getInstance('United States of America'));

But the same result occured – the GetInstance method returned null.

Edit: Further Investigation

Upon suggestion by @eyescream, I used the getAll method to retrieve my custom setting. If the getInstance and getValues didn't return what I wanted, getAll wouldn't either, right?

So I updated my Anonymous Apex to the following:

System.Debug(LoggingLevel.Info, [SELECT Name, Id FROM Country__c WHERE Name = 'United States of America']);
System.Debug(LoggingLevel.Info, Country__c.getInstance('United States of America'));

for(Country__c SingleCountry : Country__c.getAll().values())
    if(SingleCountry.Name == 'United States of America')
        System.Debug(LoggingLevel.Info, String.valueOf(SingleCountry));

The Intriguing Results

13:56:06.048 (48182000)|USER_DEBUG|[1]|INFO|(Country__c:{Name=United States of America, Id=a01a000000anByPAAU})
13:56:06.049 (49151000)|USER_DEBUG|[2]|INFO|null
13:56:06.060 (60227000)|USER_DEBUG|[6]|INFO|Country__c:{Name=United States of America, SetupOwnerId=00De0000002LT3WEAW, Country_Name__c=United States of America, LastModifiedById=00530000006MGQeAAO, SystemModstamp=2012-10-24 05:48:36, CreatedById=00530000006LZDrAAO, CreatedDate=2012-10-15 22:50:34, LastModifiedDate=2012-10-24 05:48:36, IsDeleted=false, Id=a01a000000anByPAAU, Code__c=US}

Best Answer

Is this in a sandbox post-refresh? If so, there is (or was maybe fixed by now) a bug that caused exactly what you're seeing. The only fix was to recreate the records in the custom settings.