Skip to content

Instantly share code, notes, and snippets.

@jmdohn
Created December 17, 2020 20:40
Show Gist options
  • Save jmdohn/ad0b466a886c7edc9210b60a9044ef3e to your computer and use it in GitHub Desktop.
Save jmdohn/ad0b466a886c7edc9210b60a9044ef3e to your computer and use it in GitHub Desktop.
Sobject Description made easy
String obj = 'PermissionSet';
SObjectType r = ((SObject)(Type.forName('Schema.'+obj).newInstance())).getSObjectType();
Map<String,Schema.SObjectField> fields = r.getDescribe().fields.getMap();
for(String field : fields.keyset()){
Integer padding = 50 - field.length();
system.debug(fields.get(field) + '-'.repeat(padding) + fields.get(field).getDescribe().getLabel());
}
@jamessimone
Copy link

you can also update the second line to: SObjectType r = Schema.getGlobalDescribe().get(obj);

@jmdohn
Copy link
Author

jmdohn commented Dec 17, 2020

Global describes are notoriously slow and this mechanism of describing an object is blazingly fast. Here's a good read on it.
https://salesforce.stackexchange.com/questions/218982/why-is-schema-describesobjectstypes-slower-than-schema-getglobaldescribe

@jamessimone
Copy link

That's fair - I wouldn't recommend using global describes in production-level code, typically (as I'm aware of the horrible performance); I thought this was just a helper method to be invoked on occasion in Anonymous Apex. If you're using globalDescribe to dynamically retrieve UI elements based on field names, or something, I definitely agree that it should be avoided like the plague due to the general slowness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment