You need to sign in to do that
Don't have an account?
Help Regarding Error
Hi Everyone ,
public static void ScheduledSMS()
{
List<Account> acc = [SELECT Id, Name, Mobile__c from Account where Weekly_Scheduled_SMS__c = true];
if ( acc.isEmpty() )
{
return;
}
integer count = 0;
Set<String> sendToPhones = new Set<String>();
for ( Account a: acc )
{
sendToPhones.add(a.Mobile__c);
TwilioMessageHelper.sendSMSMessage(sendToPhones,'Test SMS');
SMS_History__c smshistory = new SMS_History__c();
smshistory.Account__c= a.Id;
smshistory.Message__c = 'Test SMS ';
smshistory.Mobile_Number__c=a.Mobile__c;
insert smshistory ;
ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.INFO, ' SMS has been sent.'));
}
}
this is my apex class method . In this sendToPhones SET i am trying to add 10 mobile numbers at a time and hence it is giving error :
You have uncommitted work pending. Please commit or rollback before calling out
can anyone help me with this code how to remove this error
Rajesh,
I'm not 100% sure this will solve the problem, but there are two things I suspect of being problematic.
1. Each time through the loop, you are sending an SMS message to one more mobile number. That is, the first time through you send one message. The second time, you send two messages. The third time, three messages, etc. It could be a problem to send a message to a number a second time, while the first message is being sent.
2. You are inserting an individual SMS_History__c record each time through the loop. This is generally bad form, as you will hit the limit for DML statements if you have more than 100 accounts to process.
I've refactored the code to eliminate these two potential problems. Try it and see if the error goes away. I'm eager to know if this helps.
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
All Answers
Rajesh,
I'm not 100% sure this will solve the problem, but there are two things I suspect of being problematic.
1. Each time through the loop, you are sending an SMS message to one more mobile number. That is, the first time through you send one message. The second time, you send two messages. The third time, three messages, etc. It could be a problem to send a message to a number a second time, while the first message is being sent.
2. You are inserting an individual SMS_History__c record each time through the loop. This is generally bad form, as you will hit the limit for DML statements if you have more than 100 accounts to process.
I've refactored the code to eliminate these two potential problems. Try it and see if the error goes away. I'm eager to know if this helps.
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
Thank you Glyn Anderson the code is working fine
Great! Could you mark it as the solution? Thanks!
-Glyn