1

I have an error somewhere in my SOQL string but can't figure out where because the only message I'm getting is 'Unknown error parsing query' if I try to execute it in the Query Editor of the dev console, or System.QueryException: expecting a right parentheses, found 'OR' when I try to test the class.

Is there a tool (either online or in VS Code or as part of the SF dev console) that will let me paste in a SOQL string and highlight the error for me? Is there a limit on the number of nested ORs and ANDs in a query string? Can anybody spot the error in this string?

string q = 'SELECT  Id, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact WHERE MailingStreet != NULL AND MailingState=\'OR\' AND ((Active_Worker__c = TRUE AND L503_PIN__c != NULL) OR Has_CareWorks_Record__c = TRUE) AND (District_Update_Date__c = NULL OR Cong_District__c = NULL OR OR_HD__c = NULL OR OR_SD__c = NULL)';

thanks for your help

ETA: to clarify, i am only escaping the single quotes around the state abbreviation (\'OR\') when i'm saving it as a string variable in the apex class. when i execute in the dev console i'm not escaping the quotes. when running in the query builder i'm using

SELECT  Id,MailingStreet,MailingCity,MailingState,MailingPostalCode FROM Contact WHERE MailingStreet != NULL AND MailingState='OR' AND ((Active_Worker__c = TRUE AND L503_PIN__c != NULL) OR Has_CareWorks_Record__c = TRUE) AND (District_Update_Date__c = NULL OR Cong_District__c = NULL OR OR_HD__c = NULL OR OR_SD__c = NULL);

still getting 'Unknown error parsing query'.

  • 5
    Try running your second query in query editor in developer console by removing the semi colon at the end. I don't see any issues with your second query apart from that semi colon. If it still doesn't work then that would be strange – Vijay Ganji Mar 21 at 5:59
5

Based on this:

I try to execute it in the Query Editor of the dev console

The issue seems that you are trying to execute the query with the condition as MailingState=\'OR\' using the escape character thus resulting in error.

To be able to execute it in the Query Editor in Dev Console, you will just need to use it as below, without having to escape the value.

SELECT .... MailingState = 'OR' ....
  • i am escaping the string only when it's being saved as a string variable inside the apex class, and not escaping it when executing it in the dev console. still getting the error though – Sarah Mar 21 at 4:43
  • 2
    After your edit, I still a semi colon at the end of the query. You don’t need to use that either in query editor. – Jayant Das Mar 21 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.