2

I'm following a Batch Apex Class workbook example. In the section that sends an email to the job's submitter, I'm unable to set any values for email variables.

At this line:
mail.setToAddresses(toAddresses);

I get the error: mail.setToAddresses unexpected token ')'

What am I doing wrong? Thanks for any help.

Kevin

// Send an email to the Apex job's submitter
// notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Account Stats Status: ' + a.status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with ' + a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});

======================================================================================== This is the entire class I don't have a log file, I'm in the Eclipse editor. The error is the red 'X' next to the line of code; - Save error: unexpected token ')'. Since this is a workbook example I'll be editing the class once I can get it going to see how it works.

global class dardenAccountStats implements Database.Batchable<sObject> {

    global final String query;

    global dardenAccountStats (String q) {
        query = q;
    }

    global Database.Querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(
                    Database.BatchableContext BC, 
                    List<sObject> scope) {
        delete scope;
        Database.emptyRecycleBin(scope);
    }

    global void finish(Database.BatchableContext BC){
        AsyncApexJob a = 
            [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, 
            TotalJobItems, CreatedBy.Email 
            FROM AsyncApexJob 
            WHERE Id = :BC.getJobId()];
    }

        // Send an email to the Apex job's submitter
        // notifying of job completion.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {a.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Account Stats Status: ' + a.status);
        mail.setPlainTextBody
        ('The batch Apex job processed ' + a.TotalJobItems +
        ' batches with ' + a.NumberOfErrors + ' failures.');
        Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});
}
1
  • 1
    Could you share the full class code, and the actual error message from the debug log?
    – JimRae
    Jan 31 '14 at 17:32
1

Wow, I just realized that the email code was not within the 'global void finish' method. When I put it in the finish method it saved without error.

Learn something new each day.

Thanks

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.