5

Suppose I have in my org those Contact records:

1) FirstName = 'Ross', LastName = 'Geller'
2) FirstName = 'Rachel', LastName = 'Green'
3) FirstName = 'Monica', LastName = 'Geller'
4) FirstName = 'Ross', LastName = 'Green'

and somebody, in an apex method, passes me those two lists:

List<String> firstNameList = new List<String>{'Ross', 'Rachel'};
List<String> lastNameList = new List<String>{'Geller', 'Green'};

is there a way to query only the "desired" contacts? ('Ross Geller' and 'Rachel Green')

If I do:

List<Contact> contactList = [SELECT Id, firstName, LastName from Contact where FirstName in :firstNameList and LastName in :lastNameList];

I get:

Ross Geller
Rachel Green
Ross Green

If I do:

List<Contact> contactList = [SELECT Id, firstName, LastName from Contact where FirstName in :firstNameList or LastName in :lastNameList];

I get:

Ross Geller
Rachel Green
Monica Geller
Ross Green

1 Answer 1

1

1) Create a formula field

FullName__c = FirstName  + " "+ LastName

2) Then in your apex code create a third list:

List<String> fullNameList = new List<String>();
for(Integer i=0;i<firstNameList.size();i++){
    fullNameList.add(firstNameList[i] + ' '+ lastNameList[i]);
}

List<Contact> contactList = [SELECT Id, firstName, LastName from Contact where FullName__c in :fullNameList]
2
  • 3
    SOQL "selectivity" errors forthcoming... To avoid future problems, FullName__c = :fullNameList AND FirstName = :firstNameList AND LastName = :lastNameList.
    – sfdcfox
    Commented Aug 19, 2019 at 13:09
  • 1
    Thanks @sfdcfox I totally missed it. :) Commented Aug 19, 2019 at 13:11

You must log in to answer this question.

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