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 

sending single email to salesforce through scheduled job

Hi everyone ,

 

This is scheduled job code ,

 

global class TwilioSchedule_Weekly_SMS Implements Schedulable{
     global void Execute(SchedulableContext SC)
     {
          ScheduledSMS();
     }    
     static void ScheduledSMS()
     {
          List<Account> acc = [SELECT  Id, Mobile__c     from Account   where Weekly_Scheduled_SMS__c = true];  
          
          if(acc.size() != 0){
              for(Account a: acc){
                  TwilioMessageController obj = new  TwilioMessageController();
                  obj.messageBody = 'test test ';
                  obj.WhoId = a.Id;
                  obj.sendToPhones.add(a.Mobile__c);
                  obj.onSend();
              }
          }
     }
}

 

in this code i am sending sending sms to the accounts on scheduled basis .  After the sms is sent i want to send an  email on the particular email address with the name of the all the  accounts to whom the SMS is sent saying that SMS to all the accounts were sent successfully 

 

Thanks ,

Rajesh

GlynAGlynA

Rajesh,

 

Try something like this:

 

global class TwilioSchedule_Weekly_SMS Implements Schedulable
{
    global void Execute( SchedulableContext SC )
    {
        ScheduledSMS();
    }

    static String message = 'SMS messages were sent to the following accounts:\n';

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

        if ( acc.isEmpty() ) return;

        for ( Account a: acc )
        {
            TwilioMessageController obj = new TwilioMessageController();
            obj.messageBody = 'test test ';
            obj.WhoId = a.Id;
            obj.sendToPhones.add( a.Mobile__c );
            obj.onSend();

            message += a.Name + '\n';
        }

        Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
        emailMsg.setToAddresses( new List<String>{ 'myemail@address.com' } );
        emailMsg.setSenderDisplayName( 'TwilioSchedule_Weekly_SMS' );
        emailMsg.setSubject( 'SMS Messages Sent' );
        emailMsg.setPlainTextBody( message );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { emailMsg } );
    }
}

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

Sonam_SFDCSonam_SFDC

Hi Rajesh,

 

You can use the Outbound email feature from Salesforce to send out email to the person using a VF template that can show the list of Accounts you pull out in the ScheduledSMS() function.

 

Read more:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_forcecom_email_outbound.htm

 

Hope this helps!