Salesforce MAP with Dynamic Values
Considerations:
e.g. Account trigger is passing old and new maps.
Create a custom metadata: ObjectNames_Setting__mdt ,DeveloperName must have object Name
public void dynamicmethod(Map<Id,sobject> newAccountMap, Map<Id,sobject> oldAccountMap) {
Set<Id> resultIds = new Set<Id>();
system.debug('----------'+newAccountMap+'-----'+oldAccountMap+'----------'+newAccountMap.size()+'-----'+oldAccountMap.size());
try{
Set<Id> ids = newAccountMap.keySet();
Id firstId = new List<id> ( ids )[0];
String sObjName = firstId.getSObjectType().getDescribe().getName(); //
system.debug('---DynamicsObjName---'+sObjName); //
List<ObjectNames_Setting__mdt> MOSobj = [SELECT id,Label,Update_Fields_Name__c, FROM ObjectNames_Setting__mdt where DeveloperName =: sObjName LIMIT 1];
System.debug('+++'+MOSobj[0].Update_Fields_Name__c);
List<String> objectFields = MOSobj[0].Update_Fields_Name__c.split(',');
for (sObject l : newAccountMap.values()) {
//system.debug('=====sObject===='+ l);
for(String fieldVar : objectFields){
if (l.get(fieldVar) != oldAccountMap.get(l.Id).get(fieldVar)) {
resultIds.add(l.Id);
break;
}
}
}
}
catch(exception e)
{
System.debug('--------Exception ---'+e);
}
}