function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raksha NarayanRaksha Narayan 

Find the datatype of each field from list<string>

I have a map<string,list<string>> storing list of objects and its fields. fir example: {{opportunity= name,stageName},{account=name,city__c},{case=name,casenumber}}and so on.. From the list<string> i want to find out the datatype of each field. Is that possible?
I tried iterating on the map value as follows:
for(List<String> m: mapfil.values()){      
            Schema.DisplayType fielddataType = m.getDescribe().getType();        }
But it throws error message saying:  "Method does not exist or incorrect signature: void getDescribe() from the type List<String>".
Please help
Alain CabonAlain Cabon

Schema.SObjectField field = FsMap.get(fieldName);
 
Schema.DescribeSObjectResult d = Opportunity.sObjectType.getDescribe();
Map<String, Schema.SObjectField> FsMap = d.fields.getMap();
for(String fieldName : FsMap.keySet()) { 
    Schema.SObjectField field = FsMap.get(fieldName);
    Schema.DescribeFieldResult f = field.getDescribe();
 system.debug(fieldName + ' label:' + f.getlabel() + ' type:' + f.getType());
    if(!f.isNillable()  && !f.isDefaultedOnCreate()) {
        system.debug(fieldName + ' label:' + f.getlabel() + ' type:' + f.getType() + ' - is Required and not defaulted on create');
    }
}


 
Raksha NarayanRaksha Narayan
Based on your above response, i tried iterating on the keyvalues of the map like this: 
for(String m:mapfil.keySet()){
            Schema.DescribeSObjectResult d = m.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> FsMap = d.fields.getMap();
            for(String fieldName : FsMap.keySet()) {
                Schema.SObjectField field = FsMap.get(fieldName);
                Schema.DescribeFieldResult f = field.getDescribe();
                system.debug(fieldName + ' label:' + f.getlabel() + ' type:' + f.getType());
            }
        }
But it throws error: Variable does not exist: sObjectType. Please help here
 
Alain CabonAlain Cabon
Ok, there is a missing part indeed by starting with an object name.
 
 String selectedObject = 'Account';
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject); 
    
 Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();


 
Raksha NarayanRaksha Narayan
Hi Alain,
The below code works but this holds all the fields on opportunity,account,case. But i want it to hold only 2-3 fields for example {{opportunity= name,stageName},{account=name,city__c},{case=name,casenumber}} as it is increasing the heap size. Can you please help here? 
for(string objectname:mapfil.keySet()){
            String objType=objectname;
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType leadSchema = schemaMap.get(objType);
            Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
            for (String fieldName: fieldMap.keySet()) {
                //get all the fields label for Account Object
                String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
                //get data types for each fields
                Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
                system.debug('fielddataType'+fielddataType);
                system.debug(' label:' + fieldLabel + ' type:' + fielddataType);
            }
        }
Can you please help here? 
Alain CabonAlain Cabon
You can copy the following class.

Project of Salesforce (open source) :

https://github.com/jlantz/Cumulus/blob/dev/src/classes/UTIL_Describe.cls
 
/*******************************************
    * Gets describe maps for a new object
    ********************************************/
    static void fillMapsForObject(string objectName) {
        // get the object map the first time
        if (gd==null) gd = Schema.getGlobalDescribe();

        // get the object description
        if (gd.containsKey(objectName)) {

            if (!objectDescribes.containsKey(objectName))
                objectDescribes.put(objectName, gd.get(objectName).getDescribe());
        } else {
            throw new SchemaDescribeException('Invalid object name \'' + objectName + '\'');
        }
    }

    /*******************************************
    * Gets a field map for a new object/field pair
    ********************************************/
    private static void fillFieldMapsForObject(string objectName, string fieldName) {
        // get the object map the first time
        fillMapsForObject(objectName);

        // get the field description
        if (!fieldTokens.containsKey(objectName)) {
            fieldTokens.put(objectName, objectDescribes.get(objectName).fields.getMap());
            fieldDescribes.put(objectName, new Map<String, Schema.DescribeFieldResult>());
        }
        if (!fieldDescribes.get(objectName).containsKey(fieldName)) {
            if (fieldTokens.get(objectName).containsKey(fieldName)) {
                Schema.DescribeFieldResult dfr = fieldTokens.get(objectName).get(fieldName).getDescribe();
                fieldDescribes.get(objectName).put(fieldName, dfr);
            } else {
                throw new SchemaDescribeException('Invalid field name \'' + fieldName + '\'');
            }
        }
    }




 
/*******************************************************************************************************
    * @description Gives field type name - ID, STRING, TEXTAREA, DATE, DATETIME, BOOLEAN, REFERENCE,
    * PICKLIST, MULTIPICKLIST, CURRENCY, DOUBLE, INTEGER, PERCENT, PHONE, EMAIL
    * @param objectName the name of the object to look up
    * @param fieldName the name of the field to look up
    * @return string the name of the of the field's type
    */
    public static string getFieldType(String objectName, String fieldName) {
        // fields in our own package must not have their prefix for the Describe Field Map
        fieldName = UTIL_Namespace.StrTokenRemoveNSPrefix(fieldName);

        // make sure we have this field's schema mapped
        if (!fieldDescribes.containsKey(objectName) || !fieldDescribes.get(objectName).containsKey(fieldName))
            fillFieldMapsForObject(objectName, fieldName);

        Schema.DescribeFieldResult dfr = fieldDescribes.get(objectName).get(fieldName);
        return dfr.getType().name();
    }