Activity Forums Salesforce® Discussions What is difference between the getvalue() and getinstance method in custom settings?

  • PRANAV

    Member
    September 2, 2016 at 5:54 am

    Hi Mohit,

    • Both returns same Object when used for list custom setting. I personally prefer the getInstance() method as it makes more sense to me. You can access a record by using the value in the Name column (you cannot use the ID or other columns).

    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 example confirms that both return same .

    • getInstance() and getValues() do not always return the same object for hierarchical custom settings (although they do for list custom settings).

    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).

    Hope this helps you.

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos