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
psplrockspsplrocks 

retrieve field names for a contact

 

I want to bind the field names of a contact in a drop down list. 

How can we get all the field names for a contact in Apex. is there any soql query to do so...

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Map<String, Schema.SObjectField fieldMap = Schema.SobjectType.Contact.Contact.fields.getMap()

 

Once you have fieldMap, you can then iterate through the key set to get all the field names.

 

 

public List<SelectOption> getFieldList() { List<SelectOption> fieldNames = new List<SelectOption>(); for(String fieldName:fieldNames.keySet()) { fieldNames.add(new SelectOption(fieldName, fieldName); } return fieldNames; }

 

 I hope that this helps you out!

 

All Answers

sfdcfoxsfdcfox

Map<String, Schema.SObjectField fieldMap = Schema.SobjectType.Contact.Contact.fields.getMap()

 

Once you have fieldMap, you can then iterate through the key set to get all the field names.

 

 

public List<SelectOption> getFieldList() { List<SelectOption> fieldNames = new List<SelectOption>(); for(String fieldName:fieldNames.keySet()) { fieldNames.add(new SelectOption(fieldName, fieldName); } return fieldNames; }

 

 I hope that this helps you out!

 

This was selected as the best answer
psplrockspsplrocks
Hi,
 
Thanks for ur reply.I tried the code given by you: 
 
public class DataSynchronization
{
   Map<String, Schema.SObjectField> fieldMap = Schema.SobjectType.Contact.fields.getMap(); 
public List<SelectOption> getFieldList() {
     
    List<SelectOption> fieldNames = new List<SelectOption>();
    for(String fieldName:fieldNames.keySet()) {
        fieldNames.add(new SelectOption(fieldName, fieldName));
    }
    return fieldNames;
}

}
 
but facing an error

Error: Compile Error: Method does not exist or incorrect signature: [LIST:System.SelectOption].keySet() at line 7 column 26

 

Thanks 

psplrockspsplrocks

Hey..I am able to solve it thanks for ur help it helped me a lot to solve my problem.I went through many docs for this ..but got it from ur side at the end..thanks for ur help, thanks  a lot.

 

 I do have another  question for u do u have any idea on dynamic binding of controls in Vf page from Apex code.

 

Thanks,

shaan