I am retrieving two properties from a record (object1) on update, and I want to look up another record (object2) that has both of these properties.
So, for example:
object1record.email = '[email protected]'
object1record.name = 'bill'
If I just have one object1 record I can make query:
[SELECT id FROM object2__c WHERE email = :object1record.email AND name = :object1record.name]
But what should I do if I want to bulkify my code and look up results for multiple object1 records?
At the moment I am doing this with my object1List:
Map<String, Map<String, Boolean>> emailNameMatchMap = new Map<String, Map<String, Boolean>>();
List<String> emailList = new List<String>();
//**use object1 name and email as keys in 2 dimensional map and place email into list:
for(object1__c object1: object1List){
emailList.add(object1.email);
if(!emailNameMatchMap.containsKey(object1.name)){
emailNameMatchMap.put(object1.name, new Map<String, Boolean>());
}
emailNameMatchMap.get(object1.name).put(object1.email, true);
}
//**search for object2 which match email from list:
object__2[] object2List = [SELECT email__c, name FROM object2 WHERE email IN :emailList];
List<String> object2MatchList = new List<String>();
//** find the object 2 which have the exact email and name match using the previously created map:
for(object2__c object2: object2List){
if(emailNameMatch.get(object2.name).get(object2.email)){
object1MatchList.add(object2);
}
}
Is there an easier way of doing this with just a SOQL query?