2

How can I rewrite this query?

ERROR: Semi join sub-selects are not allowed with the 'OR' operator

SELECT Id, Name, Email FROM Contact WHERE 
ID IN (SELECT Employee_ID__c FROM Employee_Skills__c WHERE Skill_ID__r.Name LIKE 'Apex')
OR
ID IN (SELECT Employee_ID__c FROM Employee_Training__c WHERE Training_ID__r.Name LIKE 'Salesforce')
1

The simplest and most obvious route would be to query each table separately and merge in the Ids. Depending on your implementation, consuming additional queries and query rows might not be too big a deal:

Set<Id> employeeIds = new Set<Id>();
for (Employee_Skills__c skill : [/*existing skill query*/])
{
    employeeIds.add(skill.Employee_Id__c);
}
for (Employee_Training__c training : [/*existing skill query*/])
{
    employeeIds.add(training.Employee_Id__c);
}
List<Contact> employees = [SELECT Name, Email FROM Contact WHERE Id IN :employeeIds];

If the additional governor consumption is an issue, there are several ways you could roll data up from these child objects so joins are not necessary. For instance, you could use dlrs to create a rollup of unique skill/training names in a simple, declarative manner.

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.