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
PrasadVRPrasadVR 

Can we schedule emails using Apex?

Hi All,

 

can any one give me some hints on how to do this 

I wanted to send emails on hourly basis like day1 at 1PM,5PM,3PM etc.. & again in day2 at 7AM ,10AM,12PM etc......

 

Thanks in Advance 

Best Answer chosen by Admin (Salesforce Developers) 
Ashish_SFDCAshish_SFDC

Hi Naidu, 

 

Try this, 

 

global class InvoiceSenderBatch implements Database.Batchable<SObject>, Database.Stateful, Schedulable
    {

        global void execute( SchedulableContext SC )
        {
          // check to see if available slots first, otherwise re-schedule
          InvoiceSenderBatch batch = new InvoiceSenderBatch();
          Database.executeBatch( batch, 1 );
        }

        global Database.QueryLocator start(Database.BatchableContext BC)
        {
            Date datePlus10 = System.today().addDays( 10 );

            String query = 
                'Select Id, ' +
                '   Name, ' +
                'From ' +
                '   c2g__codaInvoice__c ' +
                'Where ' +
                '   c2g__InvoiceDate__c = :datePlus10 ' ;
            }

            return Database.getQueryLocator(query);
        }

        global void execute(Database.BatchableContext BC, List<SObject> scope)
        {           
          for( Sobject invoice : scope )
          {
            emailInvoice( invoice );
            invoice.put( 'Emailed__c', System.today() );
          }
          update scope;
        }


        global void finish(Database.BatchableContext BC)
        {
          // do something interesting
        }
    }

 

http://salesforce.stackexchange.com/questions/5646/scheduling-emails-in-apex

 

http://boards.developerforce.com/t5/Apex-Code-Development/Sending-email-from-scheduled-apex/td-p/344827

 

 

Regards,

Ashish

 

 

 

 

All Answers

Subhani_SFDCSubhani_SFDC

Hi,

 

Please check the following link once.

 

 

http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/

 

 

Thanks

Subhani
Salesforce Certified Developer
www.mydbsync.com

Rahul_sgRahul_sg
You can send emails in 2 ways:
1) Use workflow email alerts
2) USe Apex batch class and schedule it to run every hour ( set frequency as per your need)
If you know how many emails you want to send after the record is created then go for WF rule.
Else go for batch class.
P.S. If you are planning to send emails to non externale addresses then there are limits. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
Ashish_SFDCAshish_SFDC

Hi Naidu, 

 

Try this, 

 

global class InvoiceSenderBatch implements Database.Batchable<SObject>, Database.Stateful, Schedulable
    {

        global void execute( SchedulableContext SC )
        {
          // check to see if available slots first, otherwise re-schedule
          InvoiceSenderBatch batch = new InvoiceSenderBatch();
          Database.executeBatch( batch, 1 );
        }

        global Database.QueryLocator start(Database.BatchableContext BC)
        {
            Date datePlus10 = System.today().addDays( 10 );

            String query = 
                'Select Id, ' +
                '   Name, ' +
                'From ' +
                '   c2g__codaInvoice__c ' +
                'Where ' +
                '   c2g__InvoiceDate__c = :datePlus10 ' ;
            }

            return Database.getQueryLocator(query);
        }

        global void execute(Database.BatchableContext BC, List<SObject> scope)
        {           
          for( Sobject invoice : scope )
          {
            emailInvoice( invoice );
            invoice.put( 'Emailed__c', System.today() );
          }
          update scope;
        }


        global void finish(Database.BatchableContext BC)
        {
          // do something interesting
        }
    }

 

http://salesforce.stackexchange.com/questions/5646/scheduling-emails-in-apex

 

http://boards.developerforce.com/t5/Apex-Code-Development/Sending-email-from-scheduled-apex/td-p/344827

 

 

Regards,

Ashish

 

 

 

 

This was selected as the best answer