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
Sridhar ReddySridhar Reddy 

Need Help on Map intialization in Trigger

Can someone please explain how the Map intialization is working ? updated my doubts/question as bold text in the code.

 
trigger AccountTrigger on Account (before insert) {
    // Create a map to store Account records by their Industry
     //Below Map is intialized but still Empty

    Map<String, List<Account>> industryAccountMap = new Map<String, List<Account>>();
    
    // Iterate through the new Account records
    for (Account acc : Trigger.new) {
        if (acc.Industry != null) {
            // Check if the Industry is already a key in the map

   //How below lone is checking the map 
//(which was initialised above and looks empty)

            if (industryAccountMap.containsKey(acc.Industry)) {
                // If the key exists, add the current Account to the corresponding list
                industryAccountMap.get(acc.Industry).add(acc);
            } else {
                // If the key doesn't exist, create a new list with the Account and put it in the map
                List<Account> accountList = new List<Account>{acc};
                industryAccountMap.put(acc.Industry, accountList);
            }
        }
    }
    
    // Now you can access the grouped Account records based on their Industry using the map
    for (String industry : industryAccountMap.keySet()) {
        List<Account> accountsInIndustry = industryAccountMap.get(industry);
        
        // Perform actions on the grouped Account records
        for (Account acc : accountsInIndustry) {
            // Modify or access individual Account records within the industry
            acc.Description = 'Updated description for ' + acc.Name;
        }
    }
}

 
SubratSubrat (Salesforce Developers) 
Hello Chhakri,

Here id the explanation for whole of the trigger :

1) The trigger is defined for the Account object and it executes before the records are inserted.

2) A map called industryAccountMap is declared to store the Account records based on their Industry. This map will have String keys (Industry values) and List<Account> values (Accounts belonging to each Industry).

3) The for loop iterates through the new Account records in the Trigger.new context variable.

4) Inside the loop, it checks if the Industry of the current Account (acc.Industry) is not null.

5) If the Industry is not null, it checks if the industryAccountMap already contains the Industry as a key using the containsKey() method. This check is used to determine if there is an existing list of Accounts for that Industry in the map.

6) If the Industry key exists in the map, it retrieves the corresponding list of Accounts using industryAccountMap.get(acc.Industry). Then, it adds the current Account (acc) to that list using the add() method.

7) If the Industry key doesn't exist in the map, it creates a new List<Account> called accountList containing only the current Account. Then, it adds the accountList to the map using industryAccountMap.put(acc.Industry, accountList).

8) After the loop completes, the industryAccountMap will contain the grouped Account records based on their Industry.

9) The next loop iterates through the keys of the map using industryAccountMap.keySet(), which provides the Industries as String values.

10) Within this loop, it retrieves the list of Accounts for each Industry using industryAccountMap.get(industry), where industry is the current key.
It then performs actions on the grouped Account records within the industry. In the example code, it sets the Description field of each Account to a modified value.

By using the map, the code effectively groups the Account records based on their Industry, allowing you to perform actions or access individual records within each industry separately.


If this helps , please mark this as Best Answer.
Thank you.
Sridhar ReddySridhar Reddy
Thanks for the quick response Subrat, I got what trigger is doing exactly except one part which doesn't make sense to me is how if condition          (if (industryAccountMap.containsKey(acc.Industry)) from the code is checking  industryAccountMap has a key or not when none of the code line seems to be adding values to the map between the map declaration and if condition ? All I am curious to know is which line of the code is adding values to the map.