0
Map<string,string> salesPerCloser {get; set;}
AggregateResult[] closerResults = [SELECT Count(Id), Closer__c from opportunity where ReportRegion__c = 'Brasil' AND CreatedDate =LAST_N_DAYS:7 group by Closer__c]; 
salesPerCloser = new Map<string,string>();    
for(AggregateResult ar : closerResults)
{
    salesPerCloser.put(String.Valueof(ar.get('expr0')),String.Valueof(ar.get('closer__c')));
   system.debug(salesPerCloser); 
}
system.debug(salesPerCloser);

11:40:07:021 USER_DEBUG [7]|DEBUG|{1=Adriana S}

11:40:07:021 USER_DEBUG [7]|DEBUG|{1=Alicia Barbel}

11:40:07:022 USER_DEBUG [7]|DEBUG|{1=Barbora Novotna}

11:40:07:022 USER_DEBUG [9]|DEBUG|{1=Barbora Novotna}

If i put anothery key value page after loop its gets added correctly. Is there anything i am doing wrong? I am expecting map to have all three values. Instead of just last one. Please suggest.

1

If your Key in Map is same for all the records, it will overwrite the existing records in the Map with the same Key.

In your case the Key for all 3 records is 1 that is the reason, it is showing the latest record in the Map in your final debug.

You can go through the documentation of Map for further information.

Excerpt from the above mentioned documentation

  • Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes.
    Uniqueness of keys of all other non-primitive types, such as sObject
    keys, is determined by comparing the objects’ field values.
  • Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

put(key, value)

If the map previously contained a mapping for this key, the old value is returned by the method and then replaced.

If the key is a string, the key value is case-sensitive.

Map<String, String> colorCodes = new Map<String, String>();

colorCodes.put('Red', 'ff0000');
colorCodes.put('Red', '#FF0000');
// Red is now #FF0000

Similarly, in your case, something like below is happenning

salesPerCloser.put('1','Adriana S');
salesPerCloser.put('1','Alicia Barbel');
salesPerCloser.put('1','Barbora Novotna');
//1 is now Barbora Novotna

When using Maps, make sure you have key as unique otherwise it will override the existing values, in your case, you can switch the key value pair depending on your requirement i.e.

Replace

salesPerCloser.put(String.Valueof(ar.get('expr0')),String.Valueof(ar.get('closer__c')));

with

salesPerCloser.put(String.Valueof(ar.get('closer__c')), String.Valueof(ar.get('expr0')));

so that it gives you the count of records for each Closer__c

  • sorry it was stupid question. – Shanker Paudel Oct 3 at 6:42
  • No problem, it does happen sometimes :) – Vijay Ganji Oct 3 at 6:45

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.