[SalesForce] Multiple LIKE in SOQL

I have a batch class where I need to find all contacts with titles like director, cEO, etc. This should allow partial matches such as 'support director', 'sales director', I was trying this but is not returning the contacts. Is there any other way other than adding a bunch of OR conditions?

String[]  VIPTITLES = new String []{'\'%CEO%\',\'%CFO%\',\'%CMO%\',\'%CTO%\',\'%CIO%\',\'%COO%\',\'%VP%\',\'%DIRECTOR%\',\'%VIP%\''};

query = 'Select Id FROM CONTACT WHERE Contact.Title LIKE : VIPTITLES'

Best Answer

You can build your query by looping through each title instead:

String[]  VIPTITLES = new String []{'CEO','CFO','CMO','CTO','CIO','COO','VP','DIRECTOR','VIP'};

Boolean first = true;
query = 'Select Id FROM CONTACT';
for(String vp : VIPTITLES){
    if(!first){
        query = query + ' OR';
    } else {
        query = query + ' WHERE';
    }
    query = query + ' Contact.Title LIKE \'%' + vp + '%\'';
    first = false;
}

The query will now be:

Select Id FROM CONTACT WHERE Contact.Title LIKE '%CEO%' OR Contact.Title LIKE '%CFO%' OR Contact.Title LIKE '%CMO%'

With all titles being a separate LIKE.

If you fill in no VIPTITLES it will return:

Select Id FROM CONTACT

Related Topic