30

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

Map<Id, String> profileIds = new Map<Id, String>();
profileIds.put('00eee000000xxxx','System Administrator');
profileIds.put('00eee00000yyyyy','YYY Profile');
profileIds.put('00eee00000zzzzz','ZZZ Profile');

List<User> 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 whats wrong with the query. I tried wrapping the query with in parenthesis too, but no luck. for example

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

2 Answers 2

49

You have your parentheses wrong.

Instead of:

NOT(Field__c LIKE 'fuzzymatch')

Use:

(NOT Field__c LIKE 'fuzzymatch')
1
  • Not Like is one of the weirdest thing that one enounters in Salesforce SOQL :). Why couldn't they make it work something not like ''
    – metasync
    Commented May 1 at 15:34
3

Better to change SOQL like this, it will work

List<User> users = [SELECT Id, Name, Email, ProfileId 
FROM User WHERE (NOT Email LIKE '%@example.com') 
AND (ProfileId NOT IN:profileIds.keySet())];
1
  • -1 for code dump with no explanation. How is this different from what OP has? What makes this work when OP's query doesn't?
    – Derek F
    Commented Mar 14, 2019 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .