0

I am trying to learn Schema class. I want to get all the field names of any object using Schema.DescribeSobjectResult . Please explain with example

3

You can use the below code snippet :

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

From this, you can get all the fields of Account object. you can use similar thing for other objects also. Let me know if this helps.

1
  • it is working. but when i try to get results of all objects it is giving an error. SObjectType acc = Schema.getGlobalDescribe().get('sObject'); – sfdcuser45 Jun 16 '15 at 11:49
1

You can query all sobjects by taking all sobjects in a list first, and passing that list in the Schema.describeSObjects function like this :

set<string> gl = Schema.getGlobalDescribe().keyset();
list<String> allObjects = new list<String>();
allObjects.addAll(gl);
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(allObjects);
for(Schema.DescribeSobjectResult res : results) 
{
 map<String, Schema.SObjectField> allFields  = res.fields.getMap();
}

Let me know if this helps. Thanks

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.