Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I need to send emails based on the Number of Days.....Say suppose in my

Policy1---- Send Email should be 10 days Before Due Date & in my

Policy2----Send Email should be 5 days After Due Date...

So accordingly on the selected policy I need to Schedule the email.How can I get with Apex

Answers with examples are much appreciated

share|improve this question

2 Answers 2

I would think that you would need a schedulable (implements Schedulable interface) Batch Apex class that runs daily and checks the Due Date. When the Due Date is either 10 or 5 days before/after as stipulated above, send the email. I have made the assumption that you will also be sending your emails singularly via apex and that you know how to do that.

So, something like this should give you a good start:

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
        }
    }
share|improve this answer
    
Thanks for the Post...Vl try it –  Eagerin Sf Dec 21 '12 at 10:13

If policy1 and Policy2 is different object then you don't need to write any apex code for that, if not please elaborate your requirement

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.