0

I am trying to compare a map that contains Contact ids to the Map that holds ContactId as one of the fields that I have queried in each key-value pairs.

Map<Id, Contact> relatedContIdsMap = new Map<Id, Contact>(selectedContacts);
Map<Id, OpportunityContactRole> relatedOppConRoleMap = 
                    new Map<Id, OpportunityContactRole>([Select Id, ContactId 
                                                        From OpportunityContactRole
                                                        Where OpportunityId 
                                                        IN :relatedOppMap.keySet() ]);

How can I have a condition to compare the ContactIds of the map: relatedContIdsMap to the ContactIds that are retrieved in the map: relatedOppConRoleMap.

I have come across this: containsKey(key) from the developer docs, which seems to compare the actual key of a map, but not a particular field inside a map.

  • Just to double check are you asking how to compare values of two maps that has the same keys? – codeyinthecloud Aug 10 '18 at 16:53
  • Nope. From above maps, each map has a separate key(one with ContactId, one with OpportunityContactRole's Id). Now trying to compare the first map's key(ContactId) to the second map's field(ContactId) to see if the second map contains the particular contact record. – Austin Evans Aug 10 '18 at 16:57
2

You can't, at least not without iterating over the values of your relatedOppConRoleMap (which, at first glance, kinda defeats the purpose of storing your OpportunityContactRoles in a map in the first place).

To get around this issue, you can simply iterate over your query results and build your map by yourself (instead of using the map constructor).

Map<Id, Contact> relatedContIdsMap = new Map<Id, Contact>(selectedContacts);
Map<Id, OpportunityContactRole> relatedOppConRoleMap = new Map<Id, OpportunityContactRole>();

for(OpportunityContactRole ocr :[Select Id, ContactId From OpportunityContactRole Where OpportunityId IN :relatedOppMap.keySet()]){
    relatedOppConRoleMap.put(ocr.ContactId, ocr);
}

// You can now access the OCR records based on the contactId
for(Contact cont :relatedContIdsMap.values()){
    OpportunityContactRole myOCR = relatedOppConRoleMap.get(cont.Id);
}

// ...or do other operations, like getting rid of all of the OCRs for 
//   Contacts that you didn't previously select
relatedOppConRoleMap.keySet().retainAll(relatedContIdsMap.keySet());
  • Seems like a promising approach to work my way around. I will keep you posted with my findings. – Austin Evans Aug 10 '18 at 17:02
0

Preface

Derek F's answer is the right answer for your situation; however, I have had a similar situation in the past where I needed to find the differences between to SObject maps and do work on the end result. For my need I created the following:

Sample

public static Map<Object, SObject> getMapDiff(Map<Id, SObject> map1, Map<Id, SObject> map2) {
    Map<Id, SObject> result = new Map<Id, SObject>();

    for (Id i : map1.keySet()) {
        if (!map2.containsKey(i)) {
            result.put(i, map1.get(i));
        }
    }

    for (Id i : map2.keySet()) {
        if (!map1.containsKey(i)) {
            result.put(i, map2.get(i));
        }
    }

    return result;
}

This generates a map of SObjects that are only present in 1 of the two maps. I honestly forgot exactly why I needed it to be this way but I wanted to get this out there in case anyone needed something like it for any weird reason. This is more of a last resort approach.

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.