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
ryanschierholzryanschierholz 

Optimize Trigger with fieldMap loop

On our Closing object we have 20+ Date fields for deadlines. I've written a Trigger that will upsert those deadlines to related Events when the date is set or changed, pulling in descriptions from custom meta data type.

However, for each meta data record it loops through all fields on the Closing, which I suspect is not ideal. I suspect there is a way to only include the DATE fields to cycle through, instead of all fields. Is that possible? Here is the code:
 
String objApi = 'pba__Closing__c';
        // 	get Object field map
        Map <String, Schema.SObjectField> fieldMap =  Schema.getGlobalDescribe().get(objApi).getDescribe().fields.getMap();
        
        //	get activity descriptions meta data records for the Object
        List<Activity_Description__mdt> ads = [SELECT Id, Label, Object_API_Name__c, Field_API_Name__c, Description__c, All_Day_Event__c 
                                               FROM Activity_Description__mdt
                                               WHERE Object_API_Name__c =: objApi];
        
        // for each record in the batch
        for(pba__Closing__c cl : newClosing){
            // for each Activity Description found for the object type
            for(Activity_Description__mdt ad : ads){
                // for each field on the object
                for(Schema.SObjectField sfield : fieldMap.Values()){
                    String fieldApi = sfield.getdescribe().getname();
                    //	if the field api on the Closing matches the field api name on the Activity Description
                    if(fieldApi == ad.Field_API_Name__c) {
                        // DO LOGIC HERE                       
                        
                    }
                }   
            }
        }

 
AbhinavAbhinav (Salesforce Developers) 
Hi Ryan,

To get field of specific datatype in your case Date from sobject you can try below code.
String objType='Account';
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();
if(fielddataType == Schema.DisplayType.Date) {
//build your logic if the Field data type is Date
    
    System.debug('--->'+fieldName);
    
}

}

reference:
http://sfdcsrini.blogspot.com/2014/06/how-to-get-field-data-types-for-each.html

If it helps, Please marks it as best answer.

Thanks!
ryanschierholzryanschierholz
Thanks Abhinav, that's good to know. However, I don't feel like it adds any optimization or saves any time, but just changes the 'IF' statement logic at the end. My IF statement compares the API names, which rules out anything that doesn't match, and your IF statement compares the field type. In both scenarios, all fields are processed. I was hoping to filter by type at a higher level, so when it gets to the FOR statement, it's only looping on the date and datetime types. 

Thank you for the info!