Pulling a report of all fields in your Salesforce org
This week, a client requested a report of all fields in the whole of their Salesforce org. Since the client is in the Financial Services sector, systems are very locked down, which meant I can’t install an AppExchange tool or connect to the org with an external tool like the excellent cloudtoolkit.co by Ben Edwards, so I had to roll my own….
- Create an object to hold the data — I called mine Object Info — and add fields for the data you want to capture, for example :-
2. Create a new Apex class that implements the Batchable interface — this is needed to get past governor limits when you have a lot of objects and fields (excuse the formatting, Medium really isn’t code friendly…)
public class batchUpdate implements Database.Batchable<sObject>{ public Iterable<sObject> start(Database.BatchableContext BC) { Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); List<ObjectInfo__c> objinfos = new List<ObjectInfo__c>(); for(String keyname : gd.keyset())
{ System.debug('Object - ' + keyname); for(Schema.SObjectField fieldRes : gd.get(keyname).getDescribe().fields.getMap().values()) { Schema.DescribeFieldResult fdr = fieldRes.getDescribe(); ObjectInfo__c oi = new ObjectInfo__c(); oi.Object_Name__c = keyname; oi.Field_Name__c = fdr.getName(); oi.Field_label__c = fdr.getLabel(); oi.Field_helptext__c = fdr.getInlineHelpText(); oi.Field_Type__c = fdr.getType().name(); objinfos.add(oi); } } return objinfos; } public void execute(Database.BatchableContext BC, List<ObjectInfo__c> scope) { insert scope; } public void finish(Database.BatchableContext BC){}}
3. To populate the records, you can now go to the Developer Console, open the Execute Anonymous window and use the following code to launch the batch job :-
batchUpdate bch = new BatchUpdate();
database.executeBatch(bch,2000);
To check on the progress of the batch, you can look under Apex Jobs in Setup. Once completed, your Object Info records should be populated
4. Create a new summary report with the fields from Object Info, grouped by Object Name (yes, it’s Classic. My client hasn’t migrated. Don’t @ me)