1

I'm hoping to use Field Sets to allow users of a managed package to customize the display of a Visualforce Page. The idea is that they install the package, create their own Field Set on the one of the installed objects, and then specify that Field Set Name in a Custom Setting to display it.

The page looks something like this:

<apex:page standardController="Contact">
    <apex:variable var="fieldSetName" value="contact.Account.ns__FieldSetName__c" />
    <apex:repeat value="{!$ObjectType.ns__CustomObject__c.FieldSets[fieldSetName]}" var="f" />
</apex:page>

But, when installed, it throws errors like this:

Unknown property 'ns__CustomObject__c.fieldsets.My_Test_Field_Set'
Error is in expression '{!$ObjectType.ns__CustomObject__c.FieldSets[fieldSetName]}'
in component <apex:repeat> in page ns:visualforcepage
Error evaluating dynamic reference 'My_Test_Field_Set'

fieldsets

Problem is, the Field Set is right there! And if I create an unmanaged page like so:

<apex:page>
    <apex:repeat value="{!$ObjectType.ns__CustomObject__c.FieldSets['My_Test_Field_Set']}" var="f" />
</apex:page>

It displays without throwing an exception.

Does this mean that Managed Visualforce Pages are "forbidden" from reaching into Field Sets other than in their own namespace? What other quirky justification could there be?

5
  • 1
    Maybe the namespaces component is looking for a field set in its own namespace. So it's expecting ns__My_Test_Field_Set to exist. Have you tried making the custom setting value be "c__My_Test_Field_Set" ? I've no idea if that would work but I recall sometimes it encourages managed package to look in the default namespace, instead of in its own.
    – Dominic
    Sep 30, 2014 at 11:54
  • 1
    Sounds a bit like when trying to reference components from a managed Email Template as per this question salesforce.stackexchange.com/questions/50244/… ...it's almost as if it wants the fieldset to be in the same namespace as per Dominic's comment. Since there is form here, sounds like it might be the case for fieldsets too. Sep 30, 2014 at 12:17
  • A worthwhile and sensible comment @Dominic, thanks. Unfortunately c__My_Test_Field_Set also throws 'Unknown property' when evaluating. Was worth a try! Sep 30, 2014 at 12:18
  • 1
    You can access the fieldset in apex using something like Schema.getGlobalDescribe().get('ns__Custom_Object__c').getDescribe().fieldsets().get('My_Test_Field_Set'). I use this approach to get the field names and then populate maps with 'label' and 'value' pairs and then display to the user. My requirement was for a read-only version so I didn't need to consider too much in the way of visualforce (apex:inputfield and the like) and could just write the value to the page. Not what I'd class as a solution to your problem, but perhaps a workaround if nothing else comes up. Good luck! Sep 30, 2014 at 13:22
  • @RichardDurrant can you promote your comment to an Answer? I was about to reimplement the whole thing as a dynamic visualforce component but I think you've saved me the trouble. Sep 30, 2014 at 13:23

2 Answers 2

2

You can access the fieldset in apex using something like Schema.getGlobalDescribe().get('ns__Custom_Object__c').getDescribe().fieldsets().g‌​et('My_Test_Field_Set').

I use this approach to get the field names and then populate maps with 'label' and 'value' pairs and then display to the user. My requirement was for a read-only version so I didn't need to consider too much in the way of visualforce (apex:inputfield and the like) and could just write the value to the page.

1
  • Awesome. I can reuse the bulk of the Visualforce inside the repeat tag, but will use an accessor to 'pluck' the FieldSet from an Apex context that can actually 'see' it. Sep 30, 2014 at 21:59
1

If this Visualforce page is added to a managed package and distributed, subscribers can edit the My_Test_Field_Set field set. The logic for generating the Visualforce page remains the same, while the presentation differs based on each subscriber’s implementation. To reference a field set from a managed package, you must prepend the field set with the organization’s namespace. Using the markup above, if My_Test_Field_Set comes from an organization called X, the field set is referenced like this: {!$ObjectType.ObjectName.FieldSets.X__My_Test_Field_Set}

1
  • Appreciate your thoughtful answer Kiran. In this case, the Field Set Name varies by record (and there may be several user-defined variants) which precludes packaging them up. It's a hairy one! Sep 30, 2014 at 13:00

You must log in to answer this question.

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