function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jmarie1228jmarie1228 

SOQL Unexpected token error on simple where clause??

Hi-
I'm trying to grab a list of contact records where the ownerID of the contact is not the same as the ownerID of the account the contact belongs to so I can fix these records (I now have a trigger in place to handle this).  Using the Apex Explorer, the following code throws an "unexpected token" error on the where clause.  What the heck am I doing wrong??  Any help is GREATLY appreciated!
 
 
Code:
Select c.AccountId, c.Account.OwnerId, c.Id, c.OwnerId from Contact c  where 
c.OwnerId != c.Account.OwnerId

 
werewolfwerewolf
SOQL does not yet allow you to compare one field to another field in the where clause.  Instead, make a custom field on Contact which returns 1 if the contact's ownerId matches the account's ownerId and 0 otherwise, and then base your query criteria on whether that field is 0.
jmarie1228jmarie1228
No wonder....now I can quit banging my head against the cubicle!  Thank you!
jmarie1228jmarie1228
Question regarding your reply, I created a custom formula field that compares contact.ownerId <> account.ownerid, but I get an error telling me I have the wrong data type (it wants a boolean, but there is no boolean data type to choose)...is there another way you would accomplish this?  Do I need some type of Decode statement or am I able to cast the result to a boolean?  Thanks in advance...
werewolfwerewolf
IF( OwnerId <>  Account.OwnerId , 1, 0)
werewolfwerewolf
Which is to say that you can't make a boolean formula field but you can use that IF to convert the boolean value to a number.
jmarie1228jmarie1228
Yes, that works perfectly.  Thank you!
tonantetonante

Hi, I am having a simuilar problem where I get Unexpected Token but I am creating the query as a string in order to display the query results ion a web page using  a ApexPages.StandardController method.  The problem I am having is concatenating the string variable I get from a pick list option to the string query. Here is my code and variable subscriptionnames is the variable containing the selected   pick list option. Thanks much for your help.:

 

 String query = 'Select Id, Account__r.Id, Account__r.Name, '+
                            'Account__r.Mailing_Attention__c, '+
                            'Account__r.Mailing_Street_1__c, ' +
                            'Account__r.Mailing_Street_2__c, ' +
                            'Account__r.Mailing_Street_3__c, ' +
                            'Account__r.Mailing_State_Province__c, ' +
                            'Account__r.Mailing_Zip_Postal_Code__c, '+
                            'Name, ' +
                            'Product__r.Name, ' +
                            'Description__c, ' +
                            'Quantity__c, ' +
                            'Last_Renewal_Date__c, ' +         
                            'Number_of_Issues__c, '    +
                            'Last_Issue_Sent__c '+        
                            'From Subscription__c \'' + subscriptionnames + '\' ';  <----- Problem

tonantetonante

Folks I am sorry. I found the answer. I had forgotten to place a WHERE clause in my query string and then attach the variable using the single quote method that was listed im my last question. Once that WHERE cluse is inserted everything works fine for the ApexPages.StandardSetController() method and SOQL query.