I have a custom object C.

It has two fields F1__c and F2__c.

In my controller there is a String s.

The s contains "F1__c"

Will it be possible for me to display this field whose API name is contained in the variable s ?

public xyzcontroller 

public String s{get;set;}

public xyzcontroller()

s = "F1__c";




I tried following Vamsi's answer but no luck.

Here is my actual VF/Controller

VF Page

<apex:page Controller="TestXXXController" showHeader="false" sidebar="false">

    <apex:form id="theform">

        <apex:outputField value="{!pr.get(varstr)}"/>



public class TestXXXController {

    public XXX__c pr{get;set;}

    public String varstr{get;set;}

    public TestXXXController()

    public void init()

        XXX__c p = new XXX__c();
        String queryStr = 'Select '+CommonUsedClass.sObjectGetAllFields(p).trim()+' FROM XXX__c LIMIT 1';
        List<XXX__c> plist = new List<XXX__c>();
        plist = Database.query(queryStr);

        pr = plist[0];

        System.debug('p == > '+ pr);

        varstr = 'Schema.XXX__c.Name';



The above VF page gives me compile error as shown below

Could not resolve field 'get(varstr)' from <apex:outputField> value binding '{!pr.get(varstr)}' in page TestXXX1
  • @Vamsi - Unfortunately the answer you provided does not work :(
    – VPY
    Mar 23, 2018 at 3:50

2 Answers 2



<apex:page controller="SampleApex">
    FirstName : {!cObj['FirstName']}
    LastName : {!cObj[VarStr]}

Controller :

public class SampleApex {

    public Contact cObj {get;set;}
    String varStr;
    public SampleApex () {
        cObj = new Contact (FirstName ='JAck', LastName='Cool');

        varStr = 'LastName';        

    public String getVarStr () {
       return varStr;

Assuming you want to display the value of a field in your visualforce page, You can use sObject.get(fiedName) in apex:outputField to pass in the fieldName dynamically and get the value displayed.

<apex:outputField value="{!YourObject.get(YourFieldNameVariable)}"/>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.