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

How to query Soql not like?

Asked by Amanda Hawes May 24, 2024 11.9K views 3 answers
Share

About this question

the soql in the following code is failing. The error message is: "Compile failure on line 6, column 5: Unexpected token '<'."

Map profileIds = new Map();
profileIds.put('00eee000000xxxx','System Administrator');
profileIds.put('00eee00000yyyyy','YYY Profile');
profileIds.put('00eee00000zzzzz','ZZZ Profile');
List users = [SELECT Id, Name, Email, ProfileId FROM User WHERE NOT(Email like '%@example.com') AND ProfileId NOT IN :profileIds.keySet()];
System.debug(users.size());
for (User usr: users)
{
    System.debug(usr.id+'    '+usr.Email);
}

When I run the Query in SOQL Editor, it says "unexpected token: AND"

SELECT Id, Name, Email, ProfileId FROM User WHERE NOT(Email like '%@example.com') AND  ProfileId NOT IN ('00eee000000xxxx','00eee00000yyyyy','00eee000000zzzz')

When I remove one of the clauses from the where statement, it works. I do not understand what's wrong with the query. I tried wrapping the query with in parenthesis too, but no luck. for example

 List users = [SELECT Id, Name, Email, ProfileId FROM User WHERE (NOT(Email like '%@example.com') AND ProfileId NOT IN :profileIds.keySet())];
 List users = [SELECT Id, Name, Email, ProfileId FROM User WHERE (NOT(Email like '%@example.com') AND (ProfileId NOT IN :profileIds.keySet()))];

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 27, 2025

In Salesforce’s SOQL (Salesforce Object Query Language), the NOT LIKE operator can be used to filter records where a field does not match a specific pattern. This is particularly useful when you want to exclude records based on partial matches. Here’s how you can use it:

Syntax of NOT LIKE in SOQL:

  SELECT  FROM  WHERE  NOT LIKE ''

Key Points:

  • The NOT LIKE operator works with wildcards:
  • % matches zero or more characters.
  • _ matches a single character.
  • Use single quotes (') around the pattern.
  • NOT LIKE is case-insensitive.

Examples:

Exclude Records with a Specific Pattern:

  SELECT Name FROM Account WHERE Name NOT LIKE 'Test%'

  • This query retrieves all accounts where the Name does not start with "Test".

Exclude Records Containing Certain Words:

  SELECT Email FROM Contact WHERE Email NOT LIKE '%@example.com'

  • This fetches contacts whose email addresses do not belong to the "example.com" domain.

Combine with Other Filters:

  SELECT Name FROM Account WHERE Name NOT LIKE 'Demo%' AND Industry = 'Technology'

  • This retrieves technology accounts where the name does not start with "Demo".

Best Practices:

  • Ensure proper use of wildcards to avoid unintended exclusions.
  • Combine NOT LIKE with LIMIT or ORDER BY for better performance when querying large datasets.
  • Use LIKE or NOT LIKE only on text-based fields such as String, Email, or Picklist.

Using NOT LIKE helps fine-tune your queries, allowing precise data filtering in Salesforce!

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.