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}
  • Is this a list C/S or Hierarchy? I tried that code for a list C/S and it worked as you are expecting (i.e. getvalues() returns the same result) Jan 3, 2014 at 21:03
  • If you goto custom settings and click manage by the appropriate setting, is there actually a record with the name of 'United States of America'?
    – Eric
    Jan 3, 2014 at 21:11
  • @BritishBoyinDC - It is a custom settings list. Jan 4, 2014 at 7:27
  • @Eric - The reason I did the SOQL query using the Name field in the WHERE clause was to verify that a record with 'United States of America' existed. Jan 4, 2014 at 7:28
  • Interesting! And what does Country__c.getAll() return?
    – eyescream
    Jan 4, 2014 at 10:09

2 Answers 2


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.

  • Thanks Tim, I will check to see if that is the problem later on during the day. Jan 4, 2014 at 8:44
  • 3
    That was it! Quite a fickle bug, eh? Jan 4, 2014 at 20:17
  • Quite! Gotta love sandbox refresh quirks.
    – Tim Smith
    Jan 4, 2014 at 20:30

You do not use SQL to retrieve custom settings. You use the Static methods for the custom settings object to retrieve the values: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm.

The code you are looking for is

Country__c ctry = Country__c.getValues('United States of America');

The getValues Method return the record with the Name value equal to the text passed into the method Then to access the fields for that record simply use regular dot notation.


Custom Setting API Name: Country__c

Custom Setting Field Name: State__c ('Ohio')

Custom Setting Field Name: Name ('United States of America')

Country__c ctry = Country__c.getValues('United States of America');

system.debug(ctry.State__c); //Should be 'Ohio'
  • It's not that you don't use SOQL - the point is you don't HAVE to use SOQL to retrieve a custom setting - you can use the static methods to avoid a SOQL statement because they are cached. But if you want to order your results based on on a field on the C/S, it can be easier to query, rather than retrieve and sort the data yourself... Jan 3, 2014 at 21:06
  • Correct. You can use SQL but no need.
    – Eric
    Jan 3, 2014 at 21:10
  • I added further clarification on the problem I am having. The issue I am dealing with is that the GetInstance method is returning null despite proof that the custom setting not only exists but, can be queried by the Name field used by the GetInstance method. The SOQL query I used in the debug statement was just used to show that the custom setting was actually visible. Jan 4, 2014 at 8:41

You must log in to answer this question.

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