You need to sign in to do that
Don't have an account?
ryanschierholz
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:
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 } } } }
To get field of specific datatype in your case Date from sobject you can try below code.
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!
Thank you for the info!