1

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";

}


}

UPDATE

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">
        <h2>Questions</h2>


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

    </apex:form>
</apex:page>

Controller

public class TestXXXController {

    public XXX__c pr{get;set;}

    public String varstr{get;set;}


    public TestXXXController()
    {
        init();
    }



    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
1
  • @Vamsi - Unfortunately the answer you provided does not work :(
    – VPY
    Mar 23 '18 at 3:50
1

VF PAGE:

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

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;
    }
}
0
1

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.