0

There are a lot of examples online which detail how to leverage Schema.getGlobalDescribe to see what objects and corresponding metadata there are in Salesforce.

Instead of leveraging code like this, which iterates over all sObjectField values from a given object, is there a way that one can iterate over all Salesforce objects and corresponding fields in one go? This way, I don't need to provide the objects one by one to get the fields.

The execute anon script I have tried so far doesn't compile and i'm wondering if i'm either making an obvious mistake or whether it is possible to iterate over all object fields in this manner.

String sep = ' - '; 
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

List<String> liObjects = new List<String>();

for (Schema.SObjectType obj : gd.values()){
    Map<String, Schema.SObjectField> fieldMap = gd.get(obj).getDescribe().fields.getMap();
    for (Schema.SObjectField sField : fieldMap.values()) {
        schema.DescribeFieldResult dField = sField.getDescribe(); 
        String fieldLabel = dField.getLabel();
        String fieldAPIName = dField.getName();      
        String line = obj.getDescribe().getName() + sep + obj.getDescribe().getLabel() ;
        liObjects.add(line);
    }
}
system.debug('size of liObjects is: ' + liObjects.size());

1 Answer 1

1

Here is the working code. Actually, in given code, in for loop, at first row, you are passing Schema.SobjectType as parameter but it takes name of object type as string. Just updated that.

String sep = ' - '; 
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

List<String> liObjects = new List<String>();

for (String obj : gd.keyset()){
    Schema.DescribeSObjectResult objdescribed  = gd.get(obj).getDescribe();
    Map<String, Schema.SObjectField> fieldMap = objdescribed.fields.getMap();
    for (Schema.SObjectField sField : fieldMap.values()) {
        schema.DescribeFieldResult dField = sField.getDescribe(); 
        String fieldLabel = dField.getLabel();
        String fieldAPIName = dField.getName();      
        String line = objdescribed.getName()+ sep + objdescribed.getLabel() ;
        liObjects.add(line);
    }
}
system.debug('size of liObjects is: ' + liObjects.size());

You must log in to answer this question.

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