1

I am writing a Scheduler Class to send out mass Emails(HTML email template) based on the time I schedule, below is my code

global class pS implements Database.batchable<sObject> , Database.Stateful{

private EmailTemplate emailTemp;
global pS(){
    emailTemp = [Select Id From EmailTemplate Where Name = 'SP']; 
}

global Database.QueryLocator start(Database.BatchableContext BC){
    String query = 'Select Id, Lastname, Firstname, Email From Contact WHERE ((Account.RecordType.Id = \'01239000000Nabc\' AND Account.premier__c = False) OR (RecordType.Id = \'01239000000Ncdd\' AND Account.Status__c = "Processing")) AND Account.Name != "Test" AND Email != null AND HasOptedOutOfEmail = False';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Contact> scope){           

    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(Contact con : scope) {
         if(emailTemp != null && con.Email != null && con.Email != '') {
           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           email.setToAddresses(new List<String>{con.Email}); 
           email.setTargetObjectId(con.Id);                   
           email.setTemplateId(emailTemp.Id);
           email.setTreatTargetObjectAsRecipient(true);        
           emails.add(email);                                      
        }          
    Messaging.sendEmail(emails);
    }                
}   
global void finish(Database.BatchableContext BC){
}
}

when I run this class I receive this error

14:46:18:012 FATAL_ERROR System.QueryException: line 1:215 no viable alternative at character '"'

But when I change my "String query" to below I do not receive any error, also I am receiving emails

String query = 'Select Id, Lastname, Firstname, Email From Contact WHERE Account.RecordType.Id = \'01239000000Nabc\' AND Account.premier__c = False

What am I doing wrong and how can I fix it?

  • Do not use a string query when an inline query will work. – sfdcfox Mar 8 '18 at 23:44
2

You cannot use double quotes ("...") in SOQL. Use single quotes instead ('...'). This behavior is documented in the Force.com SOQL and SOSL Reference under fieldExpression syntax:

If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error.

  • Sorry! I didn't know that, will be careful from next time, thanks. – Lin Mar 8 '18 at 23:38
  • Thanks Adrian! I just replaced the "..." to \'...\' and it is working now my new query is String query = 'Select Id, Lastname, Firstname, Email From Contact WHERE ((Account.RecordType.Id = \'01239000000Nabc\' AND Account.premier__c = False) OR (RecordType.Id = \'01239000000Ncdd\' AND Account.Status__c = '\Processing\')) AND Account.Name != '\Test\' AND Email != null AND HasOptedOutOfEmail = False'; – Lin Mar 8 '18 at 23:38
  • I am receiving two emails, one with Attachment and the other without Attachment. It is not Plain Text as well. Any idea why is this happening? thanks. – Lin Mar 8 '18 at 23:43
  • Please do not ask entirely different questions as a Comment. Your initial issue is resolved. If you face others please pose a new Question. – Adrian Larson Mar 8 '18 at 23:47

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.