4

Are GetInstance and GetValues methods of custom setting same? According to the documentation here http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm both seem to be same. What you guys think?

  • 6
    here's some good commentary from Rich Unger, former PMTS @ Salesforce.com - Rich's Rule of Thumb When Working With Custom Settings: Use getValues() when you’re writing custom settings, and use getInstance() when reading them. – bigassforce Feb 24 '13 at 20:56
3

The getInstance() and getValues() do not always return the same object for hierarchical custom settings but for list custom settings they return the same values .

From the docs

getInstance() Returns a custom setting data set record for the current user. The fields returned in the custom setting record are merged based on the lowest level fields that are defined in the hierarchy.

getInstance() gets you the merged values for all hierarchy levels above and including its argument (so if the user with id myUserId has a null value for a field, getInstance(myUserId) can inherit a value from the user's profile or the org-wide defaults)

getValues() Returns the custom setting data set record for the specified user ID

 Foundation_Countries__c myCS1 = Foundation_Countries__c.getValues('United States');
 String myCCVal = myCS1.Country_code__c;
 Foundation_Countries__c myCS2 = Foundation_Countries__c.getInstance('United States');
 String myCCInst = myCS2.Country_code__c;
 system.assertEquals(myCCinst, myCCVal);

The above confirms that both return same for list custom settings.

  • 2
    They return the same for list custom settings, but not always for hierarchy (see Rich Unger's commentary). – Avrom Roy-Faderman Jun 2 '15 at 6:09
4

As the first comment on the question points out, getInstance() and getValues() do not always return the same object for hierarchical custom settings (although they do for list custom settings). The Rich Unger article linked there explains the difference well, but in brief:

  • getInstance() gets you the merged values for all hierarchy levels above and including its argument (so if the user with id myUserId has a null value for a field, getInstance(myUserId) can inherit a value from the user's profile or the org-wide defaults)
  • getValues() gets you the record as specified for its argument (so if the user with id myUserId has a null value for a field, getValues(userId) will always have a null value for the field)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.