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?
testLead
in this snippet? – rael_kid Sep 16 '14 at 20:54getAll()
workaround is your best bet or test for null before callinggetInstance()
. You can file a documentation feedback form. – cropredy Sep 17 '14 at 2:04