Say suppose in Object1(Project) I have records.....Now my requirement is in a vf page I have to get the Object1(Project) names for a field.....For this I have Quered like this
List<Object1__c> Objname = [select name from Object1__c];
and called this in a page like this
<apex:outputfield value="{Objname.name}"/>
But unable to get the Values displayed.....Any help pls
Attribution to: Eagerin Sf
Possible Suggestion/Solution #1
A soql query is used to retrieve the data held in object records.
To display the data held in the name field use apex:repeat to iterate over ObjName
< Apex:repeat value ="{!ObjName}" var="objVar" >
< Apex:outputText value ="{!objVar.Name}" />
< /apex:repeat >
If however you want to retrieve the field names you need a schema describe.
Schema.SObjectField> schemaFieldMap = Schema.SObjectType.Object1__c.fields.getMap();
Will give you a map of all the fields for your object. You can iterate over this map using apex:repeat to list out the field names in your VF page
Apex:repeat value="{!schemaFieldMap.keySet()}" var ="fname" Apex:outputText value ="{!fname"}" /apex:repeat
Attribution to: techtrekker
Possible Suggestion/Solution #2
Here's how to render as a selectList
Controller :
public String nameList { get; set;}
public List<SelectOption> getNames() {
List<SelectOption> options = new List<SelectOption>();
for(Object1__c obj : [Select Id, Name from Object1__c])
options.add(new SelectOption(obj.Name,obj.Name));
return options;
}
VF Page :
<apex:selectList value="{!nameList}" title="Choose a Name">
<apex:selectOptions value="{!names}"/>
</apex:selectList>
Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3568