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
AvLavAvLav 

sObject fields name locate to a selectlist

Hy!

 

I want to use the name of the sObject field in  seleclist. How can I get this name?

I read the apex guide, but I don't undestand very well the sobject describe result. I need a STRING, but the method result is a SCHEMA.DESCRIBESOBJECTRESULT.

I need something similar:

 

public List<SelectOption> getCateg() { List<SelectOption> options = new List<SelectOption>(); for (Account acc : [select id, Name from Account]) { String cat = acc.Name; options.add(new SelectOption(cat, cat)); } return options; }

 

 But I need the name of the field (accountname, type, website, etc) insted of the value.

 

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

This should work:

 

 

public List<SelectOption> getCateg() { List<SelectOption> options = new List<SelectOption>(); Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();Schema.SObjectType leadSchema = schemaMap.get('Account');Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();for (String fieldName: fieldMap.keySet()) { options.add(new SelectOption(fieldName, fieldName)); } return options;}

 /G