[SalesForce] Updating ‘multi select pick list’ field, on many contacts, with different values

My situation is this:

I have a custom object that has two custom text fields I need.
Name of a list and client Id. Each client Id can be a part of more than one list.

I then need to take this information and insert it into a custom multi-select picklist on the contact.

So: gather what lists each contact is on and insert it onto the contact page.

I can retrieve this information with this SOQL:

SELECT Contact__c, Listi__r.name FROM List_Member__c

I've been experimenting (and failing) with Lists, Sets and Maps and what could be the best way to do this.

It would make sense to use Map, where the key is the contact ID and then a string or list of strings could be the value. But so far I haven't been able to insert the list names into the value of the map, and how I would then go about updating the contacts with said values.

//Get Contact ID and List names from Object
List<List_Member__c> ConsIds = [SELECT Contact__c, Listi__r.name FROM List_Member__c];

//Create set to store Contact Ids
Set<Id> SetConIds = New Set<Id>();

//Iterate through List and fill set/map
for(List_Member__c m : ConsIds){
    SetConIds.add(m.Contact__c); 
}


for(Id i : SetConIds){
    Contact c;
    c.id = i; //Gives me error: System.NullPointerException: Attempt to de-reference a null object
}

//Maybe I can gather together contacts that I want to update, and update them
    Contact[] Contactss = [SELECT Id, Postlistar__c FROM Contact WHERE Id IN :SetConIds];

This is the basics of it, I'm quite lost at the moment and hoping for some pointers.


EDIT:
I think I've got it now, I'm just running into CPU time limits as there are too many contacts/lists and I need to turn this into a batch apex code 🙂

Solution;

//Get Contact ID and List names from Object
List<List_Member__c> ConsIds = [SELECT Contact__c, Listi__r.name FROM List_Member__c];

//Create Map to store Contact Ids and List names
Map<Id, String> MapConInfo = New Map<Id, String>();

//Iterate through Map and fill it with data
for(List_Member__c m : ConsIds){
    String listName;

    //Check if value is empty, if not then append new list to the back of string
    if(MapConInfo.get(m.Contact__c) == NULL){
        listName = m.Listi__r.name;
    }
    else{
        listName = MapConInfo.get(m.Contact__c) + ';' + m.Listi__r.name;
    }

    MapConInfo.put(m.Contact__c, listName);
}

//Collect contacts to update
Contact[] Contactss = [SELECT Id, Postlistar__c FROM Contact WHERE Id IN :MapConInfo.keySet()];

Integer count = 0;

for(Contact c : Contactss){
    if(c.Postlistar__c != MapConInfo.get(c.Id)){
        c.Postlistar__c = MapConInfo.get(c.Id);
        count++;
    }
}
Update Contactss;
System.debug('Number of Contacts updated: ' + count);

Best Answer

Use the following code to generate the Map Map conList=new Map() for(List_Member__c m : ConsIds) { if(conList.containsKey(m.contact__c)) { String newList=conList.get(m.contact__c)+';'+m.Listi__r.name; conList.put(m.contact__c,newList); } else { conList.put(m.contact__c,newList); } }

Once the map is ready you can simply get the values from it using the contact id and store the value in your custom field

for(Contact c: your query) { if(conList.containsKey(c.id)) c.Postlistar__c= conList.get(c.id); }