0

I have this Map Collection with the key of Id and the value is a List of contact. And I have a List of all contact with different Account Id.

Now, what i am trying to work is Upon looping the List of contact the accountId will be the index and add the collection to that index and if the AccountId of the Contact is already in the Map collection it will just add the collection to the specifid index of the map collection, how does it work?

My code:

Map<Id,List<Contact>> accountContactMap = new Map<Id,List<Contact>>();

    for(Contact contInsert: contactList){
        if(accountContactMap.containsKey(contInsert.AccountId)){
            accountContactMap.put(contInsert.AccountId,contInsert);
        }
    }

And also I am having an error:

Method does not exist or incorrect signature: void (Id, Contact) from the type Map<Id,List<Contact>>

Any help please?

3 Answers 3

0

There are couple of syntax errors in the code

  • Signature for adding an element to map is incorrect. It should be mapVariable.put(key,value)
  • You have declared a map with Id as key and List<Contact> as value, but you are trying to add Id as key and Contact as value which is why you are getting that error.

This is how your code should look like

Map<Id,List<Contact>> accountContactMap = new Map<Id,List<Contact>>(); 
List<Contact> conList = new List<Contact>();
for(Contact contInsert: contactList){ 
    if(accountContactMap.containsKey(contInsert.AccountId)){ 
        conList= accountContactMap.get(contInsert.AccountId);
        conList.add(contInsert);
        accountContactMap.put(contInsert.AccountId, conList); 
    } else {
        conList= new List<Contact>();
        conList.add(contInsert);
        accountContactMap.put(contInsert.AccountId, conList);
    }
}     

Note : You can optimize this code further i.e. 3 lines in if and 3 lines in else can be converted to a single line, but I have elaborated the code for better readability.

Optimized Code

Map<Id,List<Contact>> accountContactMap = new Map<Id,List<Contact>>(); 
for(Contact contInsert: contactList){ 
    if(accountContactMap.containsKey(contInsert.AccountId)){ 
        accountContactMap.get(contInsert.AccountId).add(contInsert);
    } else {  
        accountContactMap.put(contInsert.AccountId, new List<Contact> { contInsert }); 
    } 
}
0
3

You have to actually call a method. Right now you have:

myMap.(key, value)

But you need:

myMap.put(key, value)
2
  • Yeah my bad, I just realize it earlier, but thanks. Commented Feb 20, 2019 at 2:59
  • Adding to this- I think OP would also need to handle adding to a list myMap.put(key, myMap.get(key).add(value) correct?
    – shmuels
    Commented Feb 20, 2019 at 3:00
0

here is the explanation:

Let's break your question into parts to have better understanding: 1- loop over the List of contact 2- accountId will be the index and add the collection to that index 3-check if the AccountId of the Contact is already in the Mapt
4- it will add the collection to the specifid index of the map collection how does it work?

Map<Id,List<Contact>> awesomeAccountContactMap = new Map<Id,List<Contact>>(); 

//loop over the List of contact 
for(Contact contInsert: [select id,AccountId from Contact limit 1]){ 

    //check if the AccountId of the Contact is already in the Map
    if(awesomeAccountContactMap.containsKey(contInsert.AccountId)){ 

       // it will add the collection to the specifid index of the map collection
        awesomeAccountContactMap.get(contInsert.AccountId).add(contInsert);
    } 
else {  
        //accountId will be the index and add the collection to that index 
        awesomeAccountContactMap.put(contInsert.AccountId, new List<Contact> { contInsert }); 
    } 
    system.debug('awesomeAccountContactMap: '+awesomeAccountContactMap);
}
Example Output for the above code: 
accountContactMap: {0012h00000FBx99AAX=(Contact:{Id=0032h00000E6zW5AAX, AccountId=0012h00000FBx99AAX, RecordTypeId=0121N000001M3RCQAX})}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .