I have a batch process that I am using to send SMS notifications. In the execute method, I am making the callout, creating a log record and then updating scope. When I try to update scope, the record is not updated and the log record fails. If I run without updating scope, log is created as desired. My problem is that I need the scope records to up date to show that a message was sent. I've tried various patterns, but here is the basic outline:
Global class Messagebatch implements Database.batchable<sObject>,
Database.stateful,Database.allowscallouts{
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID,firstname,lastname,cellphone__c,phone,needs_sms__c FROM Account WHERE needs_sms__c = true LIMIT 25');
}
global void execute(Database.BatchableContext BC, List<Account> scope){
//list to hold logs to be inserted after loop
list<messagelog__c>messagelist= new list<messagelog__c>();
for (Account acc : scope) {
acc.needs_sms__c=false;
//class to send message here called here
//after message sent, create a record
messagelog__c log = new messagelog__c();
//fill object with response data and add to list for insert
messagelist.add(log);
}//end of loop
insert messagelist;
update scope;
}//end of execute method
My problem is that when I update scope, the message logs are not created AND scope is not updated. When I remove the "update scope;" line, message records are created correctly. I tried to call another batch in the finish method to do the update, but was not successful with this approach. My end goal here is to simply uncheck the "needs_sms__c" check box to stop a duplicate message from being sent. The messages are sent successfully each time, but I need a way to update the record after a message has been sent. Any suggestions are appreciated. Thank you!
Account
?