In Salesforce Object Query Language (SOQL), there isn’t a direct CONTAINS function, but you can achieve similar results using the LIKE operator with wildcards (%). This helps search for records where a field contains a specific substring.
Using LIKE for a "Contains" Search
To find records where a field includes a specific word or phrase, wrap the search term with %:
SELECT Id, Name FROM Account WHERE Name LIKE '%Sales%'
This query retrieves accounts with "Sales" anywhere in the Name, such as "Salesforce" or "Global Sales Inc.".
Using SOQL "Contains" in Apex Code
In Apex, you can run a SOQL query with LIKE like this:
List accounts = [SELECT Id, Name FROM Account WHERE Name LIKE '%Tech%'];for (Account acc : accounts) { System.debug(acc.Name);}
This fetches all accounts whose Name contains "Tech".
Applying "Contains" to Custom Fields
You can also use LIKE for searching in custom fields:
SELECT Id, Email FROM Contact WHERE Email LIKE '%gmail.com%'
Retrieves all contacts with a Gmail email address.
Limitations of SOQL "Contains"
- Case-sensitive – Searching "sales" won’t match "Sales".
- Cannot use LIKE on Picklist, Multi-Select Picklist, or Encrypted fields.
Alternative: Using SOSL for Full-Text Search
If you need a broader search across multiple fields, use SOSL:
FIND 'Tech' IN ALL FIELDS RETURNING Account(Id, Name)
✅ This searches for "Tech" across multiple objects and fields.