0

I have a filtered list which comprises of Countries and employees. I am putting them in a map with country as key . If country is same anywhere in the list , the employee count should get added. How can this be done?

Map < String,Headcount__c> HCMap = new Map < String,Headcount__c > ();

public void HC(string yearSelected) { 
for(Headcount__c hc: [SELECT  Country__c,Employee__c FROM Headcount__c where Year__c =: yearSelected ]) {
    HCMap.put(hc.Country__c,hc);
  }
}

So after putting it in the map , I want to get a list with non duplicate countries with total employee for each country.

Can you please help?

Adding little more info :

Intially I will be having a list like(just listed one country here) :
Country__cDepartment__c Employee__c

Mexico Marketing 15
Mexico Finance 15
Mexico Finance 15
Mexico Finance 10
Mexico Learning 10
Mexico Finance 25
Mexico Marketing 10

Finally the list would be 3 rows for each country summing up the headcount for each department

Mexico Finance (15+15+10+25)=65
Mexico Marketing (10+15)=25
Mexico Learning 10

So I assume , key should comprise of Country__c+Department__c?

1

What you need to do is put a list of Headcount__c in the value of the map:

Map < String, List<Headcount__c> > HCMap = new Map < String, List<Headcount__c> > ();

public void HC(string yearSelected) { 
  for(Headcount__c hc: [SELECT  Country__c,Employee__c,Department__c FROM Headcount__c where Year__c =: yearSelected ]) {
    if(!HCMap.containsKey(hc.Country__c + hc.Department__c))
        HCMap.put(hc.Country__c + hc.Department__c, new List<Headcount__c>());
    HCMap.get(hc.Country__c + hc.Department__c).add(hc);
  }
}

As you can see, we now have a list of Headcount__c records which gets instantiated the first time the Country__c/Department__c combination is seen in the loop. Keep in mind that after the loop, you need to check whether the map actually contains the key you're looking for before using that country's list. You can output your full list of number of headcounts per country using:

for(String key : HCMap.keyset()){
    System.debug( HCMap.get(key).size() );
}

You also have the option to see which Headcount__c's are part of which country + department combination. Another option would be to create a map in a map (Map>>) and use the second map for the departments.

  • Have put up the requirement . Do we now need to create a country+department key and iterate through the list ? Please suggest. – Vanilla_Sky Jan 16 '15 at 7:18
  • Please have a look at my updated answer. – Koen Wesselman Jan 16 '15 at 8:12
1

SOQL is your friend !

SELECT count(Id), Country__c FROM Headcount__c GROUP BY Country__c WHERE Year__c =: yearSelected

does the calculation for you. Count(Id) gives a singular entity: AggregateResult. The Apex doc is pretty straightforward and helpful.

Here may be a solution.

This is a Map you will be able to use:

map<String,String> HCMap = new map<String,String>();

We loop through the result as you did:

for (AggregateResult ar: [SELECT count(Id), Country__c FROM Headcount__c GROUP BY Country__c]) {

Results values are SObjects, so we can access the fields using their get method. We also need to cast the first and convert the calculation's result since it is an integer value:

    HCMap.put( (String) ar.get('Country__c'),  String.valueOf(ar.get('expr0')));
}

Test it in the Execute anonymous window of the Developer Console by adding this line. It provides a fast popup with the second parameter you specify, as would do system.debug:

system.assertEquals(null, HCMap);

Full block:

map<String,String> HCMap = new map<String,String>();
for (AggregateResult ar: [SELECT count(Id), Country__c FROM Headcount__c GROUP BY Country__c]) {
    HCMap.put( (String) ar.get('Country__c'),  String.valueOf(ar.get('expr0')));
}
system.assertEquals(null, HCMap);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.