Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to use SOQl contains?

Asked by Christian Parsons Jun 5, 2023 6.6K views 3 answers
Share

About this question

My SOQL Query
string str = '%'+email.subject.trim()+'%'; GMC__c gmcList = [select id,name,Case_Number__c from GMC__c where name like :str limit 1];

GMC__c Name is an autonumber field which will be unique Now my actual requirement is when a user sends a mail Subject with only Name it works perfectly. In the other case if he says suppose This is regarding GMCName then we need to Query that contains GMCName. What would be the best approach for this

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 5, 2025

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.

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 29, 2024

In SOQL, the CONTAINS operator is used to perform full-text searches on a text field. This operator allows you to search for one or more words within a specified text field of an object.


Here’s the basic syntax of the CONTAINS operator in SOQL:

SELECT field1, field2 
FROM Object 
WHERE CONTAINS(fieldname, 'searchterm')

Parameters:

  • field1, field2: The fields you want to retrieve in the query results.
  • Object: The Salesforce object you are querying.
  • fieldname: The name of the text field you want to search within.
  • 'searchterm': The term or terms you want to search for. Note that multiple search terms should be separated by spaces.

Example:

Let's say you have a custom object called Product__c, and it has a text field called Description__c. You want to search for products where the description contains the word "electronics".

SELECT Name, Description__c 
FROM Product__c 
WHERE CONTAINS(Description__c, 'electronics')

This query will return all products whose description contains the word "electronics".

Additional Considerations:

  • Case-Insensitive: By default, the CONTAINS operator is case-insensitive. This means it will match search terms regardless of case.
  • Word Boundary: The search term must match whole words in the text field. It won't match substrings within words.
  • Indexing Requirement: The field being searched must be indexed for full-text search. Text fields are indexed by default, but you may want to verify if custom fields are indexed if you encounter performance issues.
  • Search Term Limit: The search term can contain up to 4000 characters.

Example with Multiple Search Terms:

You can also search for records containing multiple terms. For example, to find products containing both "electronics" and "gadgets":

SELECT Name, Description__c 
FROM Product__c 
WHERE CONTAINS(Description__c, 'electronics AND gadgets')

This query will return products whose description contains both "electronics" and "gadgets".

Conclusion:

The CONTAINS operator in SOQL allows you to perform full-text searches on text fields within Salesforce objects. It's a powerful tool for finding records based on specific keywords or phrases contained within text fields.



Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.