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
Uves RavatUves Ravat 

Make this dynamic

Hi. I have been trying to make the following code dynamic but it kept on failing?


Any help?

journal.Enquiry_Token__c = mappedJournals.get(journal.c2g__Reference__c).Enquiry_Token__c;
journal.End_Date__c = mappedJournals.get(journal.c2g__Reference__c).End_Date__c;
journal.Start_Date__c = mappedJournals.get(journal.c2g__Reference__c).Start_Date__c;
journal.SITS_Old_and_New_Number__c = mappedJournals.get(journal.c2g__Reference__c).SITS_Old_and_New_Number__c;
journal.Sage_QB_Account__c = mappedJournals.get(journal.c2g__Reference__c).Sage_QB_Account__c;


Best Answer chosen by Admin (Salesforce Developers) 

Can you explain a bit more what do you mean by "Dynamic"?


Do you want your mapping to be dynamic. or managed through a config or something? Try something like below though not sure if this is what you are looking for:


Map<string,string> mapDynamic = new Map<string,string>{'Enquiry_Token__c' => 'Enquiry_Token__c','End_Date__c' => 'End_Date__c', 'Start_Date__c' => 'Start_Date__c', 'SITS_Old_and_New_Number__c' => 'SITS_Old_and_New_Number__c', 'Sage_QB_Account__c' => 'Sage_QB_Account__c'};

for(string key : mapDynamic.keySet())
 journal.put(key, mapDynamic.get(key));

 No whenever you change the mapping in mapDynamic, the following for loop will simply take care of dynamic mapping.