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
Singgih JuniawanSinggih Juniawan 

How to use Map on Apex using multiple key to update a field

Hi All

Can you help me to explain about MAP on apex trigger , i have 2 object and i want to get value on the other object base on multiple key on the object. please find bellow :

i want to get CPD ID and add into contract product delivery base on Key (Prospect, Prospect_Product, Sub Item, SAP ID)

User-added image

Singgih Juniawan

You create a string that concatenates all the pieces you need and use that as your key in your map, with the value being the CPD Id. Then you build that key again while looping through your Prospect Product Details and get the value from your map.

The code below will not compile, but it should give you an idea as to how to attack this problem. 

Map<String, String> complexKey2CPDId = new Map<String, String>();
String complexKey;
// loop through and build my complex key map
for (Contract_Product_Delivery__c each : myListOfCPDs) {
    complexKey = each.Prospect + each.Prospect_Product + each.SubItem + each.SAPId;
    // put this in the map with the complex key as the key and the CPD Id as the value
    complexKey2CPDId.put(complexKey, each.Id)

// loop through the Prospect Product Details and set the CPD Id
for (Prospect_Product_Detail each : myListOfPPDs) {
    complexKey2CPDId = each.Prospect + each.Prospect_Product + each.SubItem + each.SAPId;
    each.Contract_Product_Delivery__c = complexKey2CPDId.get(complexKey);