+ Start a Discussion
Gaurav AgnihotriGaurav Agnihotri 

Schema Class : how to display all the text fields in an object

I am trying to explore different possiblity from Schema class. I beleive this class is a good resource for all programmers.
I am trying to display text fields of a custom object. The script below gives me all the fields 

public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get('Account').getDescribe().fields.getMap();

How can i get the field property so that i can filter all text fields?
Synthia BeauvaisSynthia Beauvais
Hi Gaurav! 

I'm not sure if the below link will help but I use it when I would like to get full details of fields on any object. 


Hi Gaurav,

Please find the code below. Hope this helps. It included both field text as well as textarea.
List<Schema.DescribeFieldResult> listDescribeSObjectResult = new List<Schema.DescribeFieldResult>();
SObjectType objType = Schema.getGlobalDescribe().get('Account');
Map<String,Schema.SObjectField> fieldMap = objType.getDescribe().fields.getMap();

for(String  fieldName : fieldMap.keySet()){ 
	Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
	if(fielddataType.name().toUpperCase() == 'STRING' || fielddataType.name().toUpperCase() == 'TEXTAREA'){
		listDescribeSObjectResult.add(fieldMap.get(fieldName).getDescribe()) ; 

[If you got answer from my post please mark it as solution.]
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for descibe call
1) https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_describesobjects_describesobjectresult.htm
2) http://blog.jeffdouglas.com/2011/10/20/getting-salesforce-field-metadata-the-easy-way/
3) https://developer.salesforce.com/forums/?id=906F000000091p2IAA

You can access the field type in the sae way that you access the label - just use the getType method.  E.g.
Schema.DisplayType myType=fieldMap.get(fieldName).getDescribe().getType();

Try below code
public static Map<String, Schema.DescribeFieldResult> getFieldMetaData(  
  Schema.DescribeSObjectResult dsor, Set<String> fields) {

  // the map to be returned with the final data
  Map<String,Schema.DescribeFieldResult> finalMap = 
    new Map<String, Schema.DescribeFieldResult>();
  // map of all fields in the object
  Map<String, Schema.SObjectField> objectFields = dsor.fields.getMap();

  // iterate over the requested fields and get the describe info for each one. 
  // add it to a map with field name as key
  for(String field : fields){
    // skip fields that are not part of the object
    if (objectFields.containsKey(field)) {
      Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
      // add the results to the map to be returned
      finalMap.put(field, dr); 
  return finalMap;

Then call above code like below
// field to return -- skips fields not actually part of the sobject
Set<String> fields = new Set<String>{'name','annualrevenue','BADFIELD'};

Map<String, Schema.DescribeFieldResult> finalMap =  
  Utils.getFieldMetaData(Account.getSObjectType().getDescribe(), fields);

// only print out the 'good' fields
for (String field : new Set<String>{'name','annualrevenue'}) {  
  System.debug(finalMap.get(field).getName()); // field name
  System.debug(finalMap.get(field).getType()); // field type
  System.debug(finalMap.get(field).getLength()); // field length

Let us know if this will help you