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
Vignesh P 6Vignesh P 6 

I have a map Map<Id, String>.. I need to change the keys as values and values as keys.. Finally that map will be looking like this---> Map<String, Id>.. How to do this??

Best Answer chosen by Vignesh P 6
Abhishek BansalAbhishek Bansal
Hi,

Please find below the script to convert a map as per your requirement :
 
Map<Id,String> existingMap = new Map<Id,String>();
//Assuming that your existingMap is already populated with values

Map<String,Id> updatedMap = new Map<String,Id>();

for(Id KeyValue : existingMap.keySet()){
	updatedMap.put(existingMap.get(keyValue),keyValue);
}

//Now you have your new Map updatedMap with your required values.

Let me know if you need more help.

Regards,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please find below the script to convert a map as per your requirement :
 
Map<Id,String> existingMap = new Map<Id,String>();
//Assuming that your existingMap is already populated with values

Map<String,Id> updatedMap = new Map<String,Id>();

for(Id KeyValue : existingMap.keySet()){
	updatedMap.put(existingMap.get(keyValue),keyValue);
}

//Now you have your new Map updatedMap with your required values.

Let me know if you need more help.

Regards,
Abhishek
This was selected as the best answer
Vignesh P 6Vignesh P 6
Hi Abhishek Bansal,

Thank you for your help.. It's working.

Thanks,
Vignesh P