I have this statement :

Schema.DescribeFieldResult f = Schema.sObjectType.Order.fields.AccountId;

I need this to be translated to this:

 string SObjName = 'Order';
 String fld = 'AccountId';
 Schema.DescribeFieldResult f = Schema.sObjectType.[SObjName].fields.[fld];

I'm kinda new to salesforce any help would be greatly appreciated. Thank you :)


That would look like this:

public DescribeFieldResult getFieldDescribe(String sType, String fName) {
  SObjectType t = ((sobject)Type.forName('Schema.'+sType).newInstance()).getSObjectType();
  return t.getDescribe().fields.getMap().get(fName).getDescribe();

Note that this method has no error checking, so you'll want to add in any extra safeguards you think are necessary.

| improve this answer | |

You get the object describe from the global describe. Then you can call fields.getMap().

DescribeSObjectResult objectDescribe = Schema.getGlobalDescribe().get('Order');
Map<String, SObjectField> fieldMap = objectDescribe.fields.getMap();
DescribeFieldResult fieldDescribe = fieldMap.get('AccountId');

Note that the Schema. is optional here. You can also one line it if you like, but it'd be quite a long line:

DescribeFieldResult field = Schema.getGlobalDescribe().get('Order')
| 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.