22

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

3 Answers 3

38

You have your parentheses wrong.

Instead of:

NOT(Field__c LIKE 'fuzzymatch')

Use:

(NOT Field__c LIKE 'fuzzymatch')
0
2

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
    Mar 14, 2019 at 19:57
0

@Santanu. because the below was acceptable, but the lower was not. I cant answer why

WORKS: FROM Opportunity WHERE AccountId = :acId AND (NOT Name LIKE 'XXXX Opp:%') ];

DOESNT WORK FROM Opportunity WHERE AccountId = :acId AND NOT(Name LIKE 'XXXX Opp:%') ];

You must log in to answer this question.

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