5

I don't quite understand this situation and I wonder, if somebody knows where is the problem.

I' assigning in loop object as a key and simple number as his value:

Map<QuoteLineItem, Integer> mapa = new Map<QuoteLineItem, Integer>();

for (QuoteLineItem a : [SELECT Id FROM QuoteLineItem]) {
    mapa.put(a, 1);    
}

The query returns two objects: QuoteLineItem:{Id=0QL290000000NMjGAM} and QuoteLineItem:{Id=0QL290000000NMeGAM}, but map contains just:

{QuoteLineItem:{Id=0QL290000000NMjGAM}=1}

When i query also for description field, the map contains both records:

{
    QuoteLineItem:{Id=0QL290000000NMjGAM, Description=discount}=1, 
    QuoteLineItem:{Id=0QL290000000NMeGAM, Description=test apptus}=1
}

I'm aware, that key field is hashed, but these two records have different Ids, so they are not the same. And still, if we put same key into map, then the old key value should be rewritten with new value.

  • 2
    Using sObject(in your case QuoteLineItem) as key for Map is really a bad idea as mentioned here.Try to use QuoteLineItem's Id as key – Praveen May 4 '16 at 11:21
  • It is a bit weird though, the documentation specifically says that the keys are based on the values of the fields of the sObject. In case of the Id, the values are clearly different, so I would expect it to work. – rael_kid May 4 '16 at 11:42
6

The code is working absolutely fine. The issue here is the system debug which prints the map. It is printing only the first value. I modified your code to also print the size and individual elements and it prints all of them. Try the below code (you might have to replace OpportunityLineItem with QuoteLineItem)

Map<OpportunityLineItem, Integer> mapa = new Map< OpportunityLineItem, Integer>();

for (OpportunityLineItem a : [SELECT Id FROM OpportunityLineItem limit 2]) {
    mapa.put(a, 1);    
}

System.debug('mapa: ' + mapa);

System.debug('Mapa size: ' + mapa.size());

for(opportunityLineItem oli : mapa.keySet()) {
  System.debug('OLI: ' + oli);
}

Result:

mapa: {OpportunityLineItem:{Id=00k4000000tDFxyAAE}=1}
Mapa size: 2
OLI: OpportunityLineItem:{Id=00k4000000tDFxyAAE}
OLI: OpportunityLineItem:{Id=00k4000000tDJKUAA2}
  • It really works. Is it safe to use sObject as key in map then? Why it doesnt print both of them? – Martin.Kona May 4 '16 at 14:05
  • 2
    I don't know why it prints only one. Also, its not advised to use the SObject as a key because the comparision would then be based on the fields in the SObject rather than the ID of the record. – RajeshShah May 5 '16 at 6:01
-2

You are not increment the number in the loop. So it is always 1. The code should be like this.

Integer counter = 1;
Map<QuoteLineItem, Integer> mapa = new Map<QuoteLineItem, Integer>();

for (QuoteLineItem a : [SELECT Id FROM QuoteLineItem]) {
    mapa.put(a, counter);    
    counter++;
}

This will work as you expect.

  • Please read the question carefully before adding the answer. The problem is not about what counter value is stores in the map. – ajinkyah May 5 '16 at 6:01
-3

This is how you can create a map for the Quote Line Items.

Map<Id, QuoteLineItem> mapa = new Map<Id, QuoteLineItem>([select Id 
                                                            from QuoteLineItem LIMIT 2]);

Hope this works for you.

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.