+ Start a Discussion
Kaplan EladKaplan Elad 

SOQL Query to look for field contains value

I have a text field on Account, which hold domains spereated with semi colon ( e.g: gmail.com,aol.com,hotmail.com).

I want to retreive all the Accounts, where this field contains specific domain (e.g: gmail.com).

Can I do it?
Suraj GharatSuraj Gharat
Hi Kaplan,

You may use "like" keyword in your SOQL with "%" to process the comparison as "contains". For example, SELECT Id FROM Account WHERE Domains LIKE '%gmail.com%'
Kaplan EladKaplan Elad
Thanks Suraj,

This will work only when the domain is the same, but when it's a variable, it will not compile (
 Domains LIKE '%domain%'
)

I succeeded using dynamic Query :  

String soql = 'select Id, RW_Domain__c FROM Account WHERE RW_Domain__c LIKE \'%'+ domainName +'%\'' ;
Suraj GharatSuraj Gharat
Yeah, dynamic SOQL was another option and you used it wisely.

Nevertheless, we could do it through inline SOQL as well, see below code:
 
String domainName='gmail.com or whatever you want';
List<Account> lstAccounts=[select Id, RW_Domain__c FROM Account WHERE RW_Domain__c LIKE :('%'+domainName+'%')] ;