7

If I have a hierarchical custom setting:

Custom_Page__c

With one field:

Url__c

Using apex, how can I surface this field in a visualforce page?

Can I just query it as if I was querying a custom object?

14

You can access them just like you would a custom object, using SOQL. By doing that though, you aren't fully leveraging one the big advantages of using a custom setting over a custom object, no impact on governor limits.

If you use a custom setting and access it using some of the methods provided, you are retrieving your data with zero impact on the governor limits. You aren't using any of your limited SOQL queries or counting towards the limited number of query rows you can retrieve.

Custom_Page__c cp = Custom_Page__c.getOrgDefaults();
string url = cp.url__c

This returns you the org defaults for that custom setting, or the highest level.

You can also pass in the userID or the profileID if you have different settings based on profiles or individual users.

 Custom_Page__c cp = Custom_Page__c.getOrgDefaults(UserID or ProfileID);
 string url = cp.url__c

Check out some of the documentation on custom setting methods here

  • Excellent answer sfdc_ninja! Much appreciated! – Daft Feb 9 '15 at 15:07
  • No worries, glad to help :) – Chris Duncombe Feb 9 '15 at 15:10
7

While there is already a great answer, I would like to point out that in a VisualForce Page, you do not even need a controller at all to access this data, because you can leverage the $Setup global variable.

{!$Setup.Custom_Page__c.Url__c}
  • 1
    You've missed the $ character before the Setup – Oles Malkov May 25 '16 at 16:00

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.