This is a concept I am having a bit of trouble understanding. In this particular case I have debugged each piece within it and I can get the values I expect, But I am not able to iterate through them together for the desired result. I have left out code not relevant to the question.

public static List<Contact> syncList() {
//other code

Map<Id, List<Contact>> lookerByIds = methodCall('parameter');

List<Contact> sfdcList = [SELECT Id, AccountId, Email, FirstName, LastName,  FROM Contact WHERE AccountId in : lookerByIds.keySet()];
for(Contact con : sfdcList) {

      for(List<Contact> lookerList : lookerByIds.values()) {
//this is where I am having trouble. I am trying to iterate through the list from the query and the list from the map as they share the key. I understand that values returns everything
            for (Contact lookerContact : lookerList) {

                if(con.Email.equals(lookerContact.Email) || con.Alt_Email__c.equals(lookerContact.Email)) {
                    con.status__c = 'Billing Contact';
                else {
                 //other argument

I apologize if I worded this poorly. I understand that the for loops is a bad way to do this but I am honestly at a loss on how to construct this.

Sample data of the Map<Id, List<Contact>> taken from debug log.

{0010b00001azWpMAAU=(Contact:{[email protected]}), 0010b00001azgDvAAI=(Contact:{[email protected]}, Contact:{[email protected]}, Contact:{[email protected]})

  • The issue here is likely with how the lookerByIds map is being constructed. Either the values are not of type List<Contact> or the values are empty. I would expand on what is actually happening when you try to loop through the lookerByIds values. While this does seem like a pretty inefficient way to filter through whatever you're trying to do, the two for loops did not break for me when using the proper Map<Id, List<Contact>> structure. Also, might help to see what 'methodCall('parameter')' is doing. Commented Sep 10, 2019 at 22:07
  • what you want is Map<String,Contact[]> contactsByEmail = pivotByEmail(methodCall('parameter')); assuming methodCall(..) returns a list of Contacts. You'll need to write the pivotByEmail method (pro tip) - then you don't need to loop through lookerList
    – cropredy
    Commented Sep 11, 2019 at 0:37

1 Answer 1


There are two points to consider here:

  1. how the map is initialized, to know that the keys are IDs for Contacts. In principle they could be IDs for any object type, in which case sfdcList will always be empty.
  2. there's nothing to say that the lists of contacts have been initialized with contacts that have the same IDs as the keys into the map.
  • So what is going on is that I need to key the Contact by the SFDC ID, as I am comparing it to data from an external source that organizes contacts by SFDC accountId as a reference. This is why I did AccountId in Keystore, where the values would match up.
    – Randy B.
    Commented Sep 11, 2019 at 16:02

You must log in to answer this question.

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