You need to sign in to do that
Don't have an account?
Dipthi
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;
}
}
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;
}
}
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);
Still if you are having the zero result then there is data in Account.
Thanks,
Maharjan.C
All Answers
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);
Still if you are having the zero result then there is data in Account.
Thanks,
Maharjan.C