I have written a batch class for sending SMSs using SMS Magic. Everything seems fine but it is failing while insertion. Please help. Below is the batch class.
Global class BatchSmsAfterSevenDaysOFDisburmentEFS implements Database.Batchable<sObject>
{
Public date varDate = date.today();
Public string str = 'Disbursed';
list<smagicinteract__smsMagic__c> lstSmsMagic = new list<smagicinteract__smsMagic__c>();
smagicinteract__smsMagic__c objSmsMagic = new smagicinteract__smsMagic__c();
string Query = 'Select Id, Client_Name_EFS__c, Actual_Disbursement_date__c,(Select id, Account.phone, Account.Name from opportunities__r) from Application__c where Application_Status__c =\'' + str + '\'';
global Database.QueryLocator start(Database.batchableContext bc)
{
system.debug('inside start');
return database.getQueryLocator(Query);
}
global void execute(Database.batchableContext bc, list<Application__c> scope)
{
system.debug('inside Execute start' + scope);
for(Application__c ObjApplication : scope)
{
system.debug('@@@@@@@@@@@ inside application for loop');
//objSmsMagic.smagicinteract__Name__c = ObjApplication.Client_Name_EFS__c;
if(ObjApplication.Actual_Disbursement_date__c.daysbetween(varDate) == 7)
{
system.debug('@@@@@@@@ date checking');
for(opportunity objOpp : ObjApplication.opportunities__r )
{
system.debug('@@@@@@@@ inside opportunity for loop');
If(objOpp.Account.Phone.length() == 10 || objOpp.Account.Phone.isNumeric())
{
System.Debug('^^^^^Inside IFF^^^^^ ');
objSmsMagic.smagicinteract__Name__c = objOpp.Account.Name;
objSmsMagic.smagicinteract__PhoneNumber__c=objOpp.Account.Phone;
objSmsMagic.smagicinteract__SenderId__c='Transactional';
objSmsMagic.smagicinteract__external_field__c = smagicinteract.ApexAPI.generateUniqueKey();
objSmsMagic.smagicinteract__SMSText__c= LoanMessageBasedOnApplicationStatus.AfterSevenDaysofDisbursmentMessage;
lstSmsMagic.add(objSmsMagic);
system.debug('>>> >>> Inside for Second loop ' + lstSmsMagic);
}
}
}
}
try
{
if(lstSmsMagic.size()> 0)
{
system.debug('@@@@@@@@ before sending' + lstSmsMagic.size() + ' and ' + lstSmsMagic);
Database.insert(lstSmsMagic, false);
system.debug('@@@@@@@@ after sending');
}
}
catch(DmlException e)
{
system.debug('********++++++++++*******'+e.getMessage());
}
}
global void finish(Database.batchableContext bc)
{
}
}
System.isBatch()
orSystem.isFuture()
methods to resolve this error. From the error you posted, it seems like your trigger is trying to make a API call. Because this is running in batch context, you can make a synchronous callout instead of using Future method. That should resolve this error.