0

I Have Occure multiple error missing single ' quote on line number 20.

But still i am confused would this batch apex eligible for sending emails to account owner.

Global class Contact_CD implements Database.Batchable<sobject> {

global Database.QueryLocator start(Database.BatchableContext BC)
    {

 return Database.getQueryLocator('select id,Primary_Email__c,'+
                                 'OwnerId,(select id from contacts where Primary_Contact__c = false) from account'+
                                 'where Customer_Type__c != 'Affiliate' AND CDH_Organisation__c IN ('currencies Direct','TORFx','TOROz'));
    }

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


        for(account a :scope) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {a.Primary_Email__c});
            email.setSaveAsActivity(false);
            email.setTargetObjectId(A.OwnerId);
            email.setSubject('Primary check box is false');
            email.setPlainTextBody('Batch Process has completed');

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }  
    global void finish(Database.BatchableContext BC)
    {

    }
}

1 Answer 1

0

Use OwnerId When Possible

email.setToAddresses(new String[] {a.Primary_Email__c});

If this is the same as the OwnerId's email, then don't use this. It will count against your daily email limits. You do not need to specify a toAddresses if the targetObjectId is set.

Do Not Call sendEmail In Loops

You are limited to 10 email invocations in a single transaction, so this will fail if you set the batch size too large. In order to make your code more efficient, create a list at the beginning, then send the emails at the end.

    Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[0];
    for(account a :scope) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        // ...
        messages.add(email);
    }
    Messaging.sendEmail(messages);

Do Not Use Sub-Queries In Start Method

Using a sub-query in the field list causes additional processing, which can cause timeouts and/or slower batch processing. If you need to query those records, do so in the execute method.

Use Inline Queries

Unless you're building a query with dynamic filters, use the inline query statement, as it helps reduce compilation errors and typos.

return Database.getQueryLocator(
  [select Primary_Email__c, OwnerId 
   from account
   where Customer_Type__c != 'Affiliate' AND
         CDH_Organisation__c IN ('currencies Direct','TORFx','TOROz')]);
2
  • sfdcfox Emails send when account doesnt have primary contact .
    – vvpps ej
    Jan 14, 2019 at 12:21
  • Thanks again i made slightly changes & it works fine
    – vvpps ej
    Jan 15, 2019 at 7:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .