0
Schema.DescribeFieldResult field = Opportunity.StageName.getDescribe();
List<Schema.PicklistEntry> PL = field.getPicklistValues();

I am trying to do this:I want to replace field name dynamically.

String A='Stagename';
String B='Opportunity.'+StageName+'.getDescribe();'
Schema.DescribeFieldResult field = B;

I am getting error .How can I achieve this I tried following:

String Field='StageName';
SObjectType OppType = Schema.getGlobalDescribe().get('Opportunity');
Map<String,Schema.SObjectField> mfields = accountType.getDescribe().fields.getMap();
Schema.SObjectField sobjF = mfields.get(Field);
Schema.DescribeFieldResult fieldProdDiv1 = Opportunity.sobjF.getDescribe();

How to achieve this if at all it is possible?

3

Referring to blog http://blog.jeffdouglas.com/2011/10/20/getting-salesforce-field-metadata-the-easy-way/ you can get field information using a Describe call.

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;
}


// 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
}

Hope this helps :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.