Search code examples
salesforceapexsoqllwcapex-sosl

how to use four different AND condition in single soql query?


I have two soql on same object but where condition is vary by single condition mentioned below, I need to combine both and make it as single soql, can someone help me how to do with below soql after where conditions?

1st one have contact.Contact_ID__c != null and 2nd one have Roles != null.I need to combine both in single soql

1st Query:

SELECT
    Id, 
    Roles, 
    contact.contact_Id__c, 
    Account.Id, 
    account.customer_Id__c, 
    Relationship_End_Date__c, 
    IsActive
FROM
    AccountContactRecord
WHERE
    account.customer_Id__c != null  AND  contact.Contact_ID__c != null  AND  Id  in: conList

2nd Query:

SELECT
    Id, 
    Roles, 
    contact.Contact_ID__c, 
    Account.Id, 
    account.customer_Id__c, 
    Ownership__c
FROM
    AccountContactRecord
WHERE
    account.customer_Id__c != null  AND  Roles != null  AND  Id  in: conList

Solution

  • Two of the conditions are identical so it's not very hard. Try to put most selective conditions first (filter by Id, by lookup, by any indexed field), good for performance.

    where account.customer_Id__c != null and contact.Contact_ID__c != null and Id IN:conList
    +
    where account.customer_Id__c != null and Roles != null and Id IN:conList
    
    becomes
    WHERE Id IN:conList 
        AND Account.Customer_Id__c != null
        AND (Contact.Contact_ID__c != null OR Roles != null)