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
Tommy CTommy C 

Using SOQL to Map Contact IDs to Emails only returns the first Contact from the passed in list

Hi, I'm having some issues with getting the Contact IDs from their emails. I have a list of emails (around 1000 users) that I want to get their Contact IDs for. Using a VLookup table isn't the most efficient because there are over 3 million Contact records to sort through. If there's an easier way, let me know! 

I tested the code below and it returns only the first value of the list of emails. When I do the query for [SELECT id, name, email FROM Contact WHERE email IN :emails], it only returns one object. What am I doing wrong? 

String users = 'johnny@abc.com, jimmy@abc.com, jonas@abc.com'
List<String> emails = users.split(',');

public static Map<String, id> contactIDFromEmail(List<String> emails){
        Map<String, id> cm = new Map<String, id>();
        Contact[] c = [SELECT id, name, email FROM Contact WHERE email IN :emails];
        for(Contact cont : c){
            cm.put(cont.email, cont.id);
        }
        return cm;
    }
MariuszMariusz
your code only needs a small change ;) 
Try:
List<String> emails = users.split(', ');
The difference is that you will recieve e.g. "jimmy@abc.com", not " jimmy@abc.com"
 
Tommy CTommy C
Wow, that was it, I can't believe I missed that. Thanks so much for the help!