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
DipthiDipthi 

plz help : Struck with Apex class - to show Group by results

Hi awesome coders , I am not able to get what is going wrong. when i execute the below class through Annonymous window, I see no results. : AccGroupByIndustry.getAccountGrByIndMap(); 

public class AccGroupByIndustry {

// Get all Accounts with Industry as Key and List of Accounts as value
    public static Map<String,List<Account>> getAccountGrByIndMap() {
    
    Map<String, List<Account>> accountsGroupedByIndustry = new Map<String, List<Account>>();
    // Fetch all accounts with Industry
    List<Account> accountsWithIndustry = [SELECT Id, Name, Industry From Account Where Industry <> Null];
    
        for (Account acc : accountsWithIndustry) 
        {
            List<Account> accountList;
            // Check if map already contains current Industry as KEY
              If(accountsGroupedByIndustry.containsKey(acc.Industry))
              {
                 // Fetch existing list and put it back to Map
                 accountList = accountsGroupedByIndustry.get(acc.Industry);
                 accountList.add(acc);   
              }
            // If key doesn't exist, add account to new List  
            else
            {
                 // Create new List
                 accountList = new List<Account>();
                accountList.add(acc);
            }
            
            // Finally put this List into Map with Industry as Key
            accountsGroupedByIndustry.put(acc.Industry,accountList);
        }  
           return accountsGroupedByIndustry;
    }   
}
Best Answer chosen by Dipthi
Maharajan CMaharajan C
Hi Dipthi,

There is no issue in your code. am able to see the results. I hope you have saved this apex class in your org.

Execute the below line and check the debug log from a Annonymous window.  Check the open log checkbox in Annonymous window

Map<String,List<Account>> accIndMap = AccGroupByIndustry.getAccountGrByIndMap();
system.debug('accIndMap ==> ' + accIndMap);

User-added image


Still if you are having the zero result then there is data in Account.

Thanks,
Maharjan.C

All Answers

Maharajan CMaharajan C
Hi Dipthi,

There is no issue in your code. am able to see the results. I hope you have saved this apex class in your org.

Execute the below line and check the debug log from a Annonymous window.  Check the open log checkbox in Annonymous window

Map<String,List<Account>> accIndMap = AccGroupByIndustry.getAccountGrByIndMap();
system.debug('accIndMap ==> ' + accIndMap);

User-added image


Still if you are having the zero result then there is data in Account.

Thanks,
Maharjan.C
This was selected as the best answer
DipthiDipthi
It works..Thanks Maharjan !!