+ Start a Discussion
Akshay GuptaAkshay Gupta 

System.QueryException: unexpected token: WHERE

String strObjProsPrefix = Schema.MCPM_Prospect__c.getSobjectType().getDescribe().getKeyPrefix();
        String strQuery = 'Select Id, MCPM_Email_Reminder_Interaction__c, MCPM_Prospect_Email_For_Reminder__c, '+
                                  'MCPM_Premise_Lookup__c, MCPM_Prospect__c'+  
                                  'from MCPM_Interaction__c where ';
        if(strRecordId.startsWith(strObjProsPrefix)){
            strQuery += 'MCPM_Prospect__c=  \''+String.escapeSingleQuotes(strRecordId)+'\' and '+
                         'MCPM_Email_Reminder_Interaction__c  = true ' +
                                                          'ORDER BY CreatedDate DESC LIMIT 1 ';
        }                                                 
        else{
            strQuery += 'MCPM_Premise_Lookup__c= \''+String.escapeSingleQuotes(strRecordId)+'\' and ' +
                                 'MCPM_Email_Reminder_Interaction__c = true'+ 
                                                          'ORDER BY CreatedDate DESC LIMIT 1 ';
        }                                                
                                                          
        List<MCPM_Interaction__c> objInterction = Database.query(strQuery);


Hi I am getting above error, tried many thing don't know what's going wrong.
Please help,
 
Best Answer chosen by Akshay Gupta
VineetKumarVineetKumar
Put an empty space before from, "from MCPM_Interaction__c where" , looks like that is messing up the query.
Also, make sure proper spaces are put in between the literals. as a check put a print out your query to know what actually you dynamic query looks like,

All Answers

Hargobind_SinghHargobind_Singh
Hi, have you tried printing a debug of strQuery on debug log ? Can you post the result here ? 
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
String strObjProsPrefix = Schema.MCPM_Prospect__c.getSobjectType().getDescribe().getKeyPrefix();

        String strQuery = 'Select Id, MCPM_Email_Reminder_Interaction__c, MCPM_Prospect_Email_For_Reminder__c, '+
                                  'MCPM_Premise_Lookup__c, MCPM_Prospect__c '+  
                                  'from MCPM_Interaction__c  ';
								  
        if(strRecordId.startsWith(strObjProsPrefix))
		{
            strQuery += ' where  MCPM_Prospect__c=  \''+String.escapeSingleQuotes(strRecordId)+'\' and '+
                         'MCPM_Email_Reminder_Interaction__c  = true ' +
                                                          'ORDER BY CreatedDate DESC LIMIT 1 ';
        }                                                 
        else{
            strQuery += ' where MCPM_Premise_Lookup__c= \''+String.escapeSingleQuotes(strRecordId)+'\' and ' +
                                 'MCPM_Email_Reminder_Interaction__c = true'+ 
                                                          'ORDER BY CreatedDate DESC LIMIT 1 ';
        }                                                
                                                          
        List<MCPM_Interaction__c> objInterction = Database.query(strQuery);

Let us know if this will help you
VineetKumarVineetKumar
Put an empty space before from, "from MCPM_Interaction__c where" , looks like that is messing up the query.
Also, make sure proper spaces are put in between the literals. as a check put a print out your query to know what actually you dynamic query looks like,
This was selected as the best answer
Akshay GuptaAkshay Gupta
Hi Amit, 
Thank you for helping my and giving a working query.
@ Vineeth thanks for explanation