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
prakashedlprakashedl 

Unable to save the file that involves dynamic binding in Visualforce

Unable to save the file that involves dynamic binding in Visualforce. It works at times but at times it errors in the following line on my visualforce page.

 


<apex:inputField value="{!sObjectInstance[fieldName]}"/>

 

<apex:repeat value="{!fieldNameList}" var="fieldName">
             <apex:inputField value="{!sObjectInstance[fieldName]}"/>
</apex:repeat>

 

The "fieldName" is string and fieldNameList is the string list containing all field names. However, I get the following save error. sObjectInstance is a bind variable in my controller that refers to generic SObject as below. Any help in this regard is highly appreciated.

 

 

SObject sObjectInstance;

 

Save Error in Visualforce

 

Description    Path    Resource    Location    Type
Save error: Incorrect parameter for subscript. Expected Text, received core.apexpages.el.adapters.metadata.JavaMetadataELAdapter setup_requiredfields.page    line 0    Force.com save problem

Best Answer chosen by Admin (Salesforce Developers) 
prakashedlprakashedl

Thanks Navatar. Like i said, it was working earlier and i suddenly got this compilation error. Now it started working again. SO, I think it was just an interim issue. Thanks for your help again. The code I posted works as well.

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

--- --------- vf page---------------

<apex:page controller="dynamicfieldsave" >

<apex:form >

    <apex:repeat value="{!fieldnamelist}" var="fieldname">

    {!fieldname} <apex:inputField value="{!con[fieldname]}"/><br/>

    </apex:repeat>

<apex:commandButton value="Save" action="{!savecon}"/>

 

 

</apex:form>

 

</apex:page>

----------------------- Apex controller ----------------

 

public class dynamicfieldsave

{

public contact con{get;set;}

public list<string>fieldnamelist{get;set;}

public dynamicfieldsave()

{

    con=new contact();

    fieldnamelist=new list<string>();

    fieldnamelist.add('firstname');

    fieldnamelist.add('lastname');

    fieldnamelist.add('email');

   

   

}

public void savecon()

{

    insert con;

}

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

prakashedlprakashedl

Thanks Navatar. Like i said, it was working earlier and i suddenly got this compilation error. Now it started working again. SO, I think it was just an interim issue. Thanks for your help again. The code I posted works as well.

This was selected as the best answer