+ Start a Discussion
Andrew GrayAndrew Gray 

aggregate list into map

Hi - basic beginner apex question but struggling. 

The list TR2 will have duplicate values for Supplier__c. I want to end up with map with count of records for each Supplier__c.

What needs to go in the for loop to get this ? Thanks 
 
List<Trading_Relationship__c> Tr2 = [SELECT Supplier__c FROM Trading_Relationship__c WHERE Supplier__c IN :AccIds ORDER By Supplier__c ASC];

Map<ID, Integer> Map1 = new Map<ID, Integer>();
        
 for (Trading_Relationship__c eachTr : Tr2){   
            
            
        }
Best Answer chosen by Andrew Gray
Abdul KhatriAbdul Khatri
Andrew,

Solution 1:
Here is one way to handle this through Apex
Map<Id, Integer> Map1 = new Map<Id, Integer>();
gregateResult[] groupedResults
  = [SELECT Supplier__c, COUNT(Id)
      FROM Trading_Relationship__c
	  WHERE Supplier__c IN :AccIds
      GROUP BY Supplier__c];
for (AggregateResult ar : groupedResults)  {
    System.debug('Supplier__c' + ar.get('Supplier__c'));
    System.debug('Count ' + ar.get('expr0'));
    Map1.put((Id)ar.get('Supplier__c'), (Integer)ar.get('expr0'));
}
Solution 2:
If the Relationship between Trading Relationship and Supplier (which I thing is Account) is Master-Detail then you can create a Rollup Summary field on Account with Count and then in Apex you can do like this
List<Account> SupplierList = [SELECT <SupplierCount> FROM Account WHERE Id IN :AccIds];

Map<ID, Integer> Map1 = new Map<ID, Integer>();     
for (Account acct : SupplierList){   
     Map1.put(acct.Id, <SupplierCount>);
}

Let me know if this helps
 

All Answers

Abdul KhatriAbdul Khatri
Andrew,

Solution 1:
Here is one way to handle this through Apex
Map<Id, Integer> Map1 = new Map<Id, Integer>();
gregateResult[] groupedResults
  = [SELECT Supplier__c, COUNT(Id)
      FROM Trading_Relationship__c
	  WHERE Supplier__c IN :AccIds
      GROUP BY Supplier__c];
for (AggregateResult ar : groupedResults)  {
    System.debug('Supplier__c' + ar.get('Supplier__c'));
    System.debug('Count ' + ar.get('expr0'));
    Map1.put((Id)ar.get('Supplier__c'), (Integer)ar.get('expr0'));
}
Solution 2:
If the Relationship between Trading Relationship and Supplier (which I thing is Account) is Master-Detail then you can create a Rollup Summary field on Account with Count and then in Apex you can do like this
List<Account> SupplierList = [SELECT <SupplierCount> FROM Account WHERE Id IN :AccIds];

Map<ID, Integer> Map1 = new Map<ID, Integer>();     
for (Account acct : SupplierList){   
     Map1.put(acct.Id, <SupplierCount>);
}

Let me know if this helps
 
This was selected as the best answer
Maharajan CMaharajan C
Hi Andrew,

Please go with any below solution:

Solution1: 
 
List<Trading_Relationship__c> Tr2 = [SELECT Supplier__c FROM Trading_Relationship__c WHERE Supplier__c IN :AccIds ORDER By Supplier__c ASC];
Map<Id, Integer> Map1 = new Map<Id, Integer>();
for(Trading_Relationship__c eachTr : Tr2){   
	if(eachTr.Supplier__c != null)	
	{
		if(!Map1.containsKey(eachTr.Supplier__c)){
			Map1.put(eachTr.Supplier__c, 1);
		}
		else{
			Map1.put(eachTr.Supplier__c, Map1.get(eachTr.Supplier__c) + 1);
		}
	}
}
system.debug('Map1 --> '  + Map1 );

Solution2:
 
Map<Id, Integer> map1 = new Map<Id, Integer>();
for(AggregateResult ar: [SELECT COUNT(Id) cnt, Supplier__c AccId FROM Trading_Relationship__c where Supplier__c IN: AccIds GROUP BY Supplier__c] ) {
	map1.put((Id) ar.get('AccId'),  (Integer) ar.get('cnt'));
}

system.debug('map1 --> ' +  map1 );

Thanks,
Maharajan.C