1

I'm following the following tutorial:

Using field-sets with visualforce

I'd like to retrieve and populate a table's columns dynamically based on the fieldset. I would like to do this by somehow using the field set name to change which fields are retrieved.

I have the following code, here is my VF page:

<apex:page controller="FieldSetTest">
    <apex:form >
        <apex:repeat value="{!currentFieldSet}"  var="C">
            <span  class="label" style="align:right"> {!C.label}  </span>  
            <apex:inputField value="{!newCase[C.fieldPath]}" /> 
        </apex:repeat>
    </apex:form>
</apex:page>

Here is my apex:

public class FieldSetTest {

    public Case newCase { get; set; } {newCase = new Case();}
    public List<Schema.FieldSetMember> currentFieldSet {get;set;} {currentFieldSet = new List<Schema.FieldSetMember>();}

    public void FieldSetTest() {
        currentFieldSet = readFieldSet('PendingHelpdeskCase','Case');
    }

    public List<Schema.FieldSetMember> readFieldSet(String fieldSetName, String ObjectName) {
        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe();
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
        return fieldSetObj.getFields();
    }
}

I would like to use the above line:

currentFieldSet = readFieldSet('PendingHelpdeskCase','Case');

To pass in the field set name and depending on the field set, display different fields in my apex:repeat component. Right now it does not populate the page with anything, can anybody let me know what I'm doing wrong?

  • I forgot to add action="{!FieldSetTest}" to my apex:page tag and after this it works fine. I'll let somebody post an answer otherwise I'll answer it myself. Doh. – user24634 May 25 '17 at 10:32
1

It is clear that you need to call the action method FieldSetTest, because this is not a constructor, so not getting automatically called.

Please use the below code, and it will call your method on page load, and have the things populated on load of the page.

<apex:page controller="FieldSetTest" action="{!FieldSetTest}">    

Hope this helps.

| improve this answer | |
  • Thanks Nitish! What do you mean it is not a constructor ? Would you be able to add an example of what WOULD make this a constructor? – user24634 May 25 '17 at 15:42
  • 1
    public FieldSetTest() { currentFieldSet = readFieldSet('PendingHelpdeskCase','Case'); } No return type, only access modifier, and name should be same as of class. and that will make it a constructor. :) – Nitish Singhal May 25 '17 at 15:50
0

I used the following code:

Apex Controller

public class DuplicationControlController {
    private static final String FIELD_SET_NAME = 'DuplicationControl';
    private List<Schema.FieldSetMember> getFields(SobjectType sobjType) {
            Map<String, Schema.FieldSet> fsMap = sobjType.getDescribe().FieldSets.getMap();
            return fsMap.get(FIELD_SET_NAME).getFields();
        }

        public List<Schema.FieldSetMember> getLeadFields() {
            return getFields(Lead.SobjectType);
        }
    }

And part of VF page insode pageBlockTable section where sobj is var for table's list :

<apex:repeat value="{!leadFields}" var="f">
                                    <apex:column headerValue="{!f.Label}">
                                        <apex:outputField value="{!sobj[f.fieldPath]}"/>
                                    </apex:column>
                                </apex:repeat>
| improve this answer | |

Your Answer

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