I see so many examples like below; however, I want to be able to pass a string that holds 'Account' or custom object name for example. The examples I see on SF are hard coding Account.

The reason I need this is because I have a table that users could enter any table name in that they want to get get data from within my code so I cannot possibly know what tables a person has in their org, they do!

//I need something like
String myObjectName = 'Account';
Schema.DescribeSObjectResult d = myObjectName.sObjectType.getDescribe();

//FROM SF Examples
Schema.DescribeSObjectResult d = Account.sObjectType.getDescribe();
Map<String, Schema.FieldSet> FSMap = d.fieldSets.getMap();

2 Answers 2


you can pass objectname as a paramter also.

please see official documentation. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_describeSObject.htm

  • 1
    Until/unless they fix this issue, I'd avoid intentionally using DescribeSObjects more than necessary.
    – sfdcfox
    Jul 31, 2021 at 17:44
  • Thanks @sfdcfox for this information. Jul 31, 2021 at 17:52
  • Thank you all for the support. They are provide the need I wanted. You guys rock!
    – TomB
    Jul 31, 2021 at 22:11

You can do this:

String myObjectName = 'Account';
Schema.DescribeSObjectResult d = ((sObject)Type.forName('Schema',myObjectName).newInstance()).getSObjectType().getDescribe();

Unlike using Schema.getGlobalDescribe().get(myObjectName), this code runs in about a millisecond instead of hundreds or thousands of milliseconds.

This trick is also faster than using describeSObjects, as discussed here.

  • Now I have a similiar issue where I need to supply the field name dynamically as a string to get the field attributes, like Email field type, etc. But SF always shows hard coded stuff when I try to search on their site. Where the example below I don't want to hard code "Account" I want to pass "Account" to function as a string, for example. Schema.DescribeFieldResult dfr = Schema.SObjectType.Account.fields.Name;
    – TomB
    Jul 31, 2021 at 23:24
  • @TomB From your DescribeResult, check out fields.getMap(). It returns a Map<String, sObjectField> that you can check to get all the fields for an object, and the sObjectField token can get you more details, like the type of field, maximum length, etc.
    – sfdcfox
    Jul 31, 2021 at 23:58
  • I am having trouble finding good solid examples on schema stuff to be honest. I find snippets outside Saleforce's Apex Reference guide for the schema stuff. I'll try to figure out a way to interate over that map. I did prior to my second question see the fields listing in that map via debug, so if I follow you correctly, I should be able to iterate over that map to pull things out that I want to compare to. Hoping I am on the right track. Thank you.
    – TomB
    Aug 1, 2021 at 2:25
  • @TomB yes, you can use fields.getMap().values() if you want to work with the tokens directly, or a combination of fields.getMap().keySet() and then get the tokens from the string values, whichever you think would work best for you.
    – sfdcfox
    Aug 1, 2021 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .