function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam IngraSam Ingra 

Why can't I populate my map correctly?

Hi, i' m trying to populate a <String, Sobject> map but when I try to take only the last record
My code :
 
private void matchCampaigns(Map<Integer, SObject> records, Map<String, SObject> recordsToUpdate){
        
        List<CampaignMember> matchingCampaignMembers = new List<CampaignMember>();
        List<CampaignMember> matchMember = new List<CampaignMember>();
        Set<Id> contactIds = new Set<Id>();
        Set<Id> campaignIds = new Set<Id>();
        
         List<Sobject> recordsToGet = records.values();
                        system.debug(recordsToGet);
                        for(Sobject obj : recordsToGet){
                            
                            Id IdContact = (Id)obj.get('ContactId');
                            Id IdCampaign = (Id)obj.get('CampaignId');
                            
                            contactIds.add(IdContact);
                            campaignIds.add(IdCampaign);
                            
                                                                           
                        }
        system.debug(contactIds);
                            system.debug(campaignIds);
                        
                        matchingCampaignMembers = [Select id,Status 
                 from CampaignMember 
                 where ContactId IN : contactIds and CampaignId IN : campaignIds];
        system.debug('My Match List ' +matchingCampaignMembers);
        
        for(Sobject oh : matchingCampaignMembers ){
            
             recordsToUpdate.put('Id', oh);
            
            
        }
        system.debug('recordsToUpdate ' +recordsToUpdate);
                        
        
    }

as you see from the debug there are not the three records found and inserted in the "matchingCampaignMembers" list. 
what is going wrong?
I would like all the records found to be there.

Debug
Best Answer chosen by Sam Ingra
prashant rishi 6prashant rishi 6
Hi sam
It looks like your adding the key as static value ID and if you see in map the key should be unique so it will store one value or if u want store at same key then u need to store as Map<String,Lis<Soject>> but if u are using Map<String,Sobject> then it will only store one value as ur key is not unique second.

Thanks
Prashant