+ Start a Discussion
nbknbk 

Dynamic Field set in Visual Force Page

Hello Everyone,

 

Here is my requirement to render fieldset dynamically from custom object in Visual force page.

custom object structure

 

FieldSetName Object

fieldset1           Account

fieldset2           Account

 

usually we are calling fieldset in visualforce as below.

<apex:pageBlockSection title="Address">
           <apex:repeat value="{!$ObjectType.Account.FieldSets.Address}"
                    var="field">
              <apex:inputField value="{!Account[field]}" />
           </apex:repeat>
</apex:pageBlockSection>

 

As highlited fieldset name "Address" above, I need to retreive the fieldset name from custom object (ex:fieldset1) and use in repeat tag.

The visualforce page needs to be dynamic once you create a new entry in custom object and new field set data should be display in vfp without modify the vfp. Pleae share your ideas to acheive the requirement!!

 

 

FieldSetname Object

fieldset1           Account

fieldset2           Account

fieldset3          Account

 

Thanks,

Krishna

Deepak Kumar ShyoranDeepak Kumar Shyoran

Hi nbk,

 

This is really a very interesting topic I also wrote a blog on it I suggest you to visit on my blog click here to visit.This blog defiantly solves of  your problem related to dynamic field on visual force page.

 

 

 

Don't forget to give Kudos if this post helped you.

 

Mark my answer as a solution to your question if it solve your problem.

 

 

 

 

 

Best Regards,

 

Choudhary Deepak Shyoran

 

Salesforce Developer

 

nbknbk

Hello Depak,

 

Thanks for reply.

I copied your code and  executed the page, by clicking on "click to show" getting errors. I hope you are retreving the fields list by clicking on button with respective Object. My requirement is related to render dynamic fieldset. I will try other alternatives and use your code and let you know if I get any solution.

 

Thanks,

krishna

 

 

Deepak Kumar ShyoranDeepak Kumar Shyoran

Hi nbk,

 

I suggest to you to read that blog and to understand the logic i.e. how to display the dynamic field on visualforce page and I think that code  is enough to understand that logic but as you say there is some error it might be possible that it shows some error on your org because it works fine for me.So please tell me that problem so that I can fixed it out.

 

----

Choudhary Deepak Shoyran

Salesforce Developer

nbknbk

Hello Depak,

 

Thanks for reply.

Please have a look in below components which I tried wtih Dynamic Fieldset and written comments in VFP. Can you please check my comments section and provide your inputs.

 

 

Apex Class:

global class Client_AC_ConfigData
{
    //required properties initialization
        global Account supplier{get;set;}
        global Integer countofConfigData {get;set;}
        global String FieldsetConfigData {get;set;}
        global List<Config_Data__c> lstConfig{ get; set; }
        // declaration
        global Client_AC_ConfigData(ApexPages.StandardController controller)
            {
                supplier = (Account)controller.getRecord();
                getDetails();
                
            }
        global list<Config_Data__c> getConfig()
        {
            list<Config_Data__c> lstConfigData = [select Fieldset_Name__c from Config_Data__c];
              return lstConfigData;
        }
        //retreive data from Config Data
         global void getDetails()     
         {

           //Created dummy records in Config Data object and use the fieldsetname in VFP
             string query = 'select id,Fieldset_Name__c,Object_Name__c from Config_Data__c';
             lstConfig = Database.query(query);
         }
}

 

VFP:

 

<apex:page standardController="Account" extensions="Client_AC_ConfigData">
   <apex:form >
       <apex:pageBlock ><apex:outputText value="{!lstConfig}"></apex:outputText>
       <apex:pageBlock title="test">
       <apex:repeat value="{!lstConfig}" var="cn">
           <apex:pageBlockSection columns="1">
               <apex:outputLabel value="{!cn.Fieldset_Name__c}" id="test"></apex:outputLabel>
<!-- by using the merge field as highlited above, I am able to get the fieldset name from custom object, i.e Config Data-->

                         <apex:repeat value="{!$ObjectType.Account.FieldSets.DemoFieldset}" var="GS">  
<!-- My intention is to use merge field(cn.Fieldset_Name__c) in the fieldset name
DemoFieldset
Getting syntax error while saving the page and I hope the salesforce does't support the merge field to call the fieldset dynamically. I tried below way, Please suggest if you have any ideas to use fieldset name dynamically.

<apex:repeat value="{!$ObjectType.Account.FieldSets.!cn.Fieldset_Name__c}" var="GS">  
-->
 <apex:inputField required="true" value="{!supplier[GS]}"/> </apex:repeat> </apex:pageBlockSection> </apex:repeat> <apex:commandButton value="Save" action="{!save}"/> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>

 

 

Deepak Kumar ShyoranDeepak Kumar Shyoran

Hi nbk,

 

As you said that you got an error while trying to save your code by removing the comments from here  <!-- <apex:repeat value="{!$ObjectType.Account.FieldSets.!cn.Fieldset_Name__c}" var="GS">  --> that bacause this a wrong way to merge to SObject such thing are not supported in salesforce.This is something like you are trying to fetch another object from the fieldset on one object.

Merge DML is only perform on the same type SObject upto 3 level.Please visit to the following link for more help on mere DML http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_merge.htm .

 

 

Don't forget to give Kudos if this post helped you.

Mark my answer as a solution to your question if it solve your problem.

 

 -----

Choudhary Deepak Shyoran

Salesforce Developer

Avidev9Avidev9

There are somethings that are lil bit mysterious and you learn them by seeing the common syntax

 

Try this

 

<apex:repeat value="{!$ObjectType.Account.FieldSets[cn.Fieldset_Name__c]}" var="GS"> 

         <apex:inputField required="true" value="{!supplier[GS.fieldPath]}"/>

</apex:repeat>