[SalesForce] Odd behavior with Custom Setting getInstance() Method

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.

LeadZipCode__c.getInstance(newLead.PostalCode)

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?

Best Answer

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.