2

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

2
  • Please be more specific here .Do you need Field names of an object or records in a table Commented Oct 22, 2012 at 8:06
  • Only Field Names here....I mean only names of the project Object...
    – Eagerin Sf
    Commented Oct 22, 2012 at 8:23

2 Answers 2

1

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>
0
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

Reference http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

1
  • Thanks Tech It worked out...Can I get values into a picklist now
    – Eagerin Sf
    Commented Oct 22, 2012 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .