In an Org I'm working with we have a Custom Setting that holds a number of Zip Codes. The zip codes are populated in the Name field of the Custom Setting, with no other fields.

We use the Lead PostalCode value with the getInstance() method to check if the Lead has a special zip code.

I ran into some odd behavior where if the PostalCode field was null, passing it into the getInstance method yielded one of the Custom Setting zip codes.

This is a sample of the code in question.


Making the following tests yielded these results.

Lead newLead = new Lead();
System.debug( newLead.PostalCode );  => null
System.debug( LeadZipCode__c.getInstance( '' ) ); => null
//System.debug( LeadZipCode__c.getInstance( null ) ); => Error at runtime Variable does not exist: LeadZipCode__c
System.debug( LeadZipCode__c.getInstance(newLead.PostalCode) ); => LeadZipCode__c:{Name=23227, SetupOwnerId=

It seems passing in a null field actually brings back a result. I've tested it with different types of fields always to get the same result.

Can anybody explain why this happens?

  • What is the value of testLead in this snippet? Commented Sep 16, 2014 at 20:54
  • testLead should have been newLead, edited the question to fix my typo.
    – Minutiae
    Commented Sep 16, 2014 at 20:58
  • That is a strange behavior. Are you getting the same Pincode everytime to pass a blank string to getInstance()?
    – A-d
    Commented Sep 16, 2014 at 21:17
  • Yep, I confirmed this as well. It is most likely retrieving the first record in the internal data structure used to hold custom settings (which is not a sorted linear list). The getAll() workaround is your best bet or test for null before calling getInstance(). You can file a documentation feedback form.
    – cropredy
    Commented Sep 17, 2014 at 2:04
  • @A-d Yes, I am always receiving the same one each time. I believe crop1645 is correct about it simply retrieving the first record in the internal data structure.
    – Minutiae
    Commented Sep 17, 2014 at 12:18

3 Answers 3


After some digging, the only thing I could find is that passing a null value to the getInstance() and getValue() returning a value is a bug, but this was back in Aug, 2013. Not able to find any known issues though and since it's code related, without premier support, I don't believe it can be reported as a bug either. Suggestion is to null check the value. Original question/answer found here.

  • The odd thing is that attempting to pass a null directly (ie LeadZipCode__c.getInstance( null ); gives me a compile error "Variable does not exist: LeadZipCode__c". I'm going to mark your answer as correct. This is a bug, and the value should be checked for null first.
    – Minutiae
    Commented Sep 17, 2014 at 12:24

While not being able to provide a reason why, I would suggest using LeadZipCode__c.getAll() and then check the map.

Lead newLead = new Lead();
Map<String, LeadZipCode__c> lzcMap = LeadZipCode__c.getAll();
System.debug( newLead.PostalCode );
System.debug( LeadZipCode__c.getInstance( '' ) );
//System.debug( LeadZipCode__c.getInstance( null ) ); => Error at runtim
System.debug( LeadZipCode__c.getValues(newLead.PostalCode) ); // Not null
system.debug(lzcMap.get(newLead.PostalCode)); // null
  • testLead was a typo. Meant to put newLead. I have edited the question to correct this.
    – Minutiae
    Commented Sep 16, 2014 at 20:59
  • As for the "full accounting", there is nothing more that needs to be added. I created that second code block as a test using Dev console, Anonymous Apex to get the results listed above.
    – Minutiae
    Commented Sep 16, 2014 at 21:02

Ok I may be shooting in dark here, but my thought is that Salesforce uses IsNull() function while checking whether getInstance()'s parameter is null or not.

My suspicion is even more strong based on the fact that IsNull() function does not support text fields and hence Salesforce's documents like this suggest users to use IsBlank()

Use ISBLANK instead of ISNULL in new formulas. ISBLANK has the same functionality as ISNULL, but also supports text fields

You must log in to answer this question.

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