Created
December 17, 2020 20:40
-
-
Save jmdohn/ad0b466a886c7edc9210b60a9044ef3e to your computer and use it in GitHub Desktop.
Sobject Description made easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
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
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
you can also update the second line to:
SObjectType r = Schema.getGlobalDescribe().get(obj);