function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dilip_VDilip_V 

Dynamically Add a Configurable List of Fields to a Visualforce Page

while searching on Google about dynamic VF pages I found this blog (http://opfocus.com/blog/how-to-dynamically-add-a-configurable-list-of-fields/).
I created the custom settings and added few fields and records.But code is not working properly.

I am getting this error:
Could not resolve field 'NewOpp' from <apex:inputField> value binding '{!dummyOpp[FieldName]}' in page customsettingsvf 

thanks.
Best Answer chosen by Dilip_V
Vivek_PatelVivek_Patel
Hi Dilip,

Your name field has value "NewOpp", as per the code you have written, it expects a opportunity field name in place of "NewOpp"

All Answers

Vivek_PatelVivek_Patel
Hi Dilip,

Can you please share the complete code?
Dilip_VDilip_V
<apex:page controller="DynamicBindingController">
     <apex:pageMessages id="pageMessages" />
     <h1>These fields are speicified from a custom list</h1>
     <apex:form >
           <apex:pageBlock mode="edit">
                <apex:pageBlockButtons >
                      <apex:commandButton value="Save" action="{!createOpportunities}" />
                </apex:pageBlockButtons>
                <apex:pageBlockSection title="Dynamic fields" columns="1" showHeader="true">
                     <apex:repeat value="{!fieldNames}" var="FieldName" >
                          <apex:pageBlockSectionItem >
                               <apex:outputLabel value="{!FieldName}" />
                               <apex:inputField value="{!dummyOpp[FieldName]}" />
                          </apex:pageBlockSectionItem>
                     </apex:repeat>
               </apex:pageBlockSection>
          </apex:pageBlock>
     </apex:form>
</apex:page>
Controller:
public without sharing class DynamicBindingController {
   public Opportunity dummyOpp {get; set;} 
   public List<String> fieldNames {get; set;}

   public DynamicBindingController(){
      //Initialize the placeholder Opportunity
      dummyOpp = new Opportunity();

      //Now let's get the List of fields from our custom setting
      fieldNames = new List<String>();
      for(Opportunity_Dynamic_Field_List__c fieldName : Opportunity_Dynamic_Field_List__c.getall().values()){
         fieldNames.add(fieldName.Name);
      }
   }

   public PageReference createOpportunities(){
      try{
         //Create the new opportunity and re-direct to it
         insert dummyOpp;
         return new PageReference('/'+dummyOpp.id);
      }catch(Exception ex){
         ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
         ApexPages.addMessage(msg); 
      }
      return null;
   }

    public Opportunity getOpportunity(String id){
       String soql = 'Select id ';
       for(Opportunity_Dynamic_Field_List__c fieldName : Opportunity_Dynamic_Field_List__c.getall().values()){
          soql = soql + ', ' + fieldName.Name;
       }
       soql = soql + ' From Opportunity where id = :id';
       return (Opportunity)Database.Query(soql);
    }
}

befor running code Need to create custom settings.

use a list custom setting Opportunity_Dynamic_Field_List__c which has 4 entries in it: “Name”, “CloseDate”, “Description”, and “StageName”.
Create few records in custom settings and then try the code.

Here you can find more info about that:
http://opfocus.com/blog/how-to-dynamically-add-a-configurable-list-of-fields/
Vivek_PatelVivek_Patel
The issue looks related to you the values of the custom setting, value in the name field of the custom setting is not the field name of opportunity, just paste the vlaues here to avoid confusion.
Dilip_VDilip_V
I tried that but no result.
Does the Issue resolved in your org.
Please let me if that is working in your org or not.

share your email address I will share my dev org credentials.
vdilipkumar7@gmail.com


 
Vivek_PatelVivek_Patel
vivek.patel188@gmail.com
Vivek_PatelVivek_Patel
Hi Dilip,

Your name field has value "NewOpp", as per the code you have written, it expects a opportunity field name in place of "NewOpp"
This was selected as the best answer