+ Start a Discussion
Monishan MMonishan M 

System.QueryException: unexpected token: Id

Hi All,

I have below code, when executing receiving the error

System.QueryException: unexpected token: Id

Code
global class RetrieveMedicalInquiryStatusBatchMVN implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful, Schedulable {

    String query = 'SELECT Id,Account_vod__c, Status_vod__c, MA_Response_AZ__c ' +
                    // Query Closing
                    'FROM Medical_Inquiry_vod__c ' +
                    'WHERE Sent_to_GMIP_MVN__c = TRUE ' +
                    'AND Status_vod__c IN :statuses' +
                    'AND Id IN :Ids';

    List <String> statuses;
    List<String>Ids = new List<String>{'a0t1y0000005QRTAA2','a0t1y0000005QRYAA2'};

Can you please let me know how to correct them

Thanks,
Monisha
Maharajan CMaharajan C
Hi Monsisha,

Space is missing in the AND condition in where clause before id AND.

String query = 'SELECT Id,Account_vod__c, Status_vod__c, MA_Response_AZ__c ' +
                    // Query Closing
                    'FROM Medical_Inquiry_vod__c ' +
                    'WHERE Sent_to_GMIP_MVN__c = TRUE ' +
                    'AND Status_vod__c IN :statuses' +
                    ' AND Id IN :Ids';   // Add the empty Space here before AND or above line.

Thanks,
Maharajan.C
Amit AherAmit Aher

Hi Monishan M,

The spaces in each concatenated string matters here. You need to add a space at the beginning of each concatenated SOQL string.

Please use the below String query
 

List<String> statusList;
List<String> idList = new List<String>{'a0t1y0000005QRTAA2','a0t1y0000005QRYAA2'};

String query = 'SELECT Id,Account_vod__c, Status_vod__c, MA_Response_AZ__c ' +
                       ' FROM Medical_Inquiry_vod__c ' +
                       ' WHERE Sent_to_GMIP_MVN__c = TRUE ' +
                       ' AND Status_vod__c IN :statusList' +
                       ' AND Id IN :idList';

Tip: Variable names must start with lower case letters. In your case Ids must be the ids or idList.

Thanks,
Amit Aher