0

I am trying to create a batch job that sends a notification to a Contact to complete a work order which was due a week ago and if the custom field Resolution__c is empty. I've written the below code in the developer console and it shows me the errors:

  1. Unexpected token 'global'.
  2. Class WorkOrderNotificationBatchJob must implement the method: void Database.Batchable.execute(Database.BatchableContext, List)

Please advise what am I doing wrong here?

    global class WorkOrderNotificationBatchJob implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
         
        Date d = Date.today()-7;
        String query = [SELECT WorkOrderNumber,CreatedDate, Resolution__c FROM WorkOrder__c WHERE Resolution__c = null AND CreatedDate = :d];
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<WorkOrder__c> workorders) {
        for(WorkOrder__c wo:workorders){
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            
            String messageBody;
            
            List<String> workordstring =new List<String>();
            
            message.setSubject('Please complete Work Order');
            message.setToAddresses(new List<String>{wo.Contact.Email__c});
            String allstring = String.join(workordstring, '<br/>');
            message.setHtmlBody('<html><body>Hi Hope you are well <br>'+'<br> List of Work Orders <br>'+allstring + ',<br>Your Work Order is not complete. <br><br><b>Regards,</b><br>System Administrator D<br/></body></html>'); 
            mailList.add(message);
            Messaging.sendEmail(mailList);
        
    global void finish(Database.BatchableContext BC){
            
        }
    }
}
}
  • 2
    are you sure you close the execute method properly? if you pasted as-is, then definitely its needing immediate attention to the brackets. – Ysr Shk Sep 9 '20 at 6:36
  • You also have an illegal assignment to string in the start method. You cannot pass the query to a string variable this way. – arut Sep 9 '20 at 9:42
0

Looking at the class, you have used the finish method within the execute method body. Try placing it correctly.

    global class WorkOrderNotificationBatchJob implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
         
        Date d = Date.today()-7;
        String query = [SELECT WorkOrderNumber,CreatedDate, Resolution__c FROM WorkOrder__c WHERE Resolution__c = null AND CreatedDate = :d];
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<WorkOrder__c> workorders) {
        for(WorkOrder__c wo:workorders){
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            
            String messageBody;
            
            List<String> workordstring =new List<String>();
            
            message.setSubject('Please complete Work Order');
            message.setToAddresses(new List<String>{wo.Contact.Email__c});
            String allstring = String.join(workordstring, '<br/>');
            message.setHtmlBody('<html><body>Hi Hope you are well <br>'+'<br> List of Work Orders <br>'+allstring + ',<br>Your Work Order is not complete. <br><br><b>Regards,</b><br>System Administrator D<br/></body></html>'); 
            mailList.add(message);
            Messaging.sendEmail(mailList);
        
        }
    }
    global void finish(Database.BatchableContext BC){
            
    }   
}

Also, instead of sending an email in the FOR loop, use a static variable to hold the list of emails and send it in the FINISH method. Makes for a more efficient coding practise.

  • thank you for helping me to find the wrong parts in my code. I used your advice to use a static variable and hold the list of emails and send it in the FINISH method. Somehow it didn't work for me. – Asel Vazirova Sep 9 '20 at 23:44
  • @AselVazirova - batch class must implement Database.Stateful in order for finish() to reference static variable set in execute()s – cropredy Sep 10 '20 at 0:53

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.