function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@rajeshdulhai@rajeshdulhai 

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

Error is in expression '{!ScheduledSMS}' in component <apex:page> in page testsendsms
 

can anyone help me with this code how to remove this error 
Best Answer chosen by Admin (Salesforce Developers) 
GlynAGlynA

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.

 

public static void ScheduledSMS()
{
    List<Account> acc = [SELECT Id, Name, Mobile__c from Account where Weekly_Scheduled_SMS__c = true];

    if ( acc.isEmpty() ) return;

    Set<String> sendToPhones = new Set<String>();

    for ( Account a: acc ) sendToPhones.add( a.Mobile__c );

    TwilioMessageHelper.sendSMSMessage( sendToPhones, 'Test SMS' );

    ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.INFO, ' SMS has been sent.') );

    List<SMS_History__c> list_SMS_Histories = new List<SMS_History__c>();

    for ( Account a: acc )
    {
        list_SMS_Histories.add
        (   new SMS_History__c
            (   Account__c          = a.Id,
                Message__c          = 'Test SMS ',
                Mobile_Number__c    = a.Mobile__c
            )
        );
    }
    insert list_SMS_Histories;
}

 

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

GlynAGlynA

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.

 

public static void ScheduledSMS()
{
    List<Account> acc = [SELECT Id, Name, Mobile__c from Account where Weekly_Scheduled_SMS__c = true];

    if ( acc.isEmpty() ) return;

    Set<String> sendToPhones = new Set<String>();

    for ( Account a: acc ) sendToPhones.add( a.Mobile__c );

    TwilioMessageHelper.sendSMSMessage( sendToPhones, 'Test SMS' );

    ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.INFO, ' SMS has been sent.') );

    List<SMS_History__c> list_SMS_Histories = new List<SMS_History__c>();

    for ( Account a: acc )
    {
        list_SMS_Histories.add
        (   new SMS_History__c
            (   Account__c          = a.Id,
                Message__c          = 'Test SMS ',
                Mobile_Number__c    = a.Mobile__c
            )
        );
    }
    insert list_SMS_Histories;
}

 

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

This was selected as the best answer
@rajeshdulhai@rajeshdulhai

Thank you Glyn Anderson the code is working fine 

GlynAGlynA

Great!  Could you mark it as the solution?  Thanks!

 

-Glyn