3

<Long intro that can be skipped>

I have a VFP that displays output from a standard controller. It's a very simple setup with nothing particularly difficult in it however it does allow the user to select which column to sort on and whether to sort ascending or descending.

The problem is that I am trying to store the user sort options so that when the revisit the page the view already uses their preferred sort settings and this is driving me up the wall. Salesforce already does this; if you go to their "Accounts | All accounts" view then click on the "Category" column name the list will be sorted by category. If you then go somewhere else in SFDC and then click on "Accounts | All accounts" again your sort option is retained, the list is still sorted by "Category".

This is exactly the functionality I want to have and I was directed to look at Hierarchy custom settings to do this.

</Long intro ...>

Looking at all the examples and documentation that is available I have tried a number of different ways of trying to store the sort options the users has selected, both with HierarchySetting and also with CustomSetting, but I get the same error every time which leads me to believe that I am missing something very basic and obvious. I have taken the example code from the Apex Developers Guide and it still produces the same error:

    ID OwnerId = UserInfo.getOrganizationId();
    Hierarchy__c CS = Hierarchy__c.getInstance(OwnerId);
    System.Assert(CS.OverrideMe__c == 'Fluffy');
    System.assert(CS.DontOverrideMe__c == 'World');

returns an error of "Invalid type: Hierarchy__c"

I have tried this with "getValues" but it returns the same error:

    //HierarchySetting__c CS = Hierarchy__c.getValues(OwnerID);
    sortOrderNew = CS.supportView_NewCases_SortOrder; if (sortOrderNew == null) { sortOrderNew = 'case.CreatedDate'; }

I need to be able to create these settings if they don't exist, within Apex rather than having to delve in to "My Settings". I then need to be able to retrieve and update these settings as required, again from within Apex.

I understand that things ending in __c are customer-generated so I even tried instantiating "HierarchySetting" first but that failed with the same "Invalid type" error.

A simple piece of code showing me how to create, retrieve and update a setting would be very much appreciated, or am I simply going about this the wrong way?

4
  • Have you actually set up the custom setting and verified its API Name?
    – Adrian Larson
    Aug 18, 2016 at 12:25
  • Also, check out this blog post by Daniel Hoechst.
    – Adrian Larson
    Aug 18, 2016 at 12:29
  • @Adrian, I have not set up any custom setting, this is part of what I am after. Aug 18, 2016 at 13:33
  • I am after a simple piece of code that allows me to create a custom setting from inside my Apex controller if it doesn't already exist and allows me to read and update that setting if it does. Aug 18, 2016 at 13:34

1 Answer 1

4

Your code will not work until you actually configure the custom setting.

  1. Navigate to Setup > Develop > Custom Settings.
  2. Click the New button.
  3. Populate the Label, for example Hierarchy Setting.
  4. Populate the Object Name, for example HierarchySetting.
  5. Leave the Setting Type as Hierarchy (default).
  6. Click the Save button.

Once you follow the above steps, you can use the Object Name as in your above code.

HierarchySetting__c orgDefaults = HierarchySetting__c.getOrgDefaults();

Same goes for Custom Fields, you can't reference them until you configure them.

5
  • Would everyone who uses my VFP and Apex code have to do this? Aug 18, 2016 at 14:00
  • No it's a one time deal to set it up. There has to be a schema reference. If you've never configured the setting on a particular instance, there won't be one.
    – Adrian Larson
    Aug 18, 2016 at 14:02
  • OK, that sounds hopeful. presumably I will have to make the same change on the production instance if/when it is ready to be moved across? Aug 18, 2016 at 14:17
  • Just deploy via Change Set. First just select the Custom Setting, then click Add/View Dependencies and it will show you all the fields. If you add more than just the Custom Setting, that second step will often contain a lot of noise.
    – Adrian Larson
    Aug 18, 2016 at 14:18
  • That sounds great, thanks for the pointers! I will go create these custom settings and see what happens. Aug 18, 2016 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .