Skip to main content
THUNDER CLOUD ha preguntado en #Apex
My org has 10000 employees and I want to send email on their birthday. How to automate this process?
5 respuestas
  1. 9 may 2016, 13:51
    Hi,

    You can achieve the above scenario by using Batch Apex and Schedule Apex. Please check the below sample code.

    Batch Class

     

    global class BatchWishes implements Database.Batchable<Employee__c> {

    global Iterable<employee__c> start(Database.BatchableContext bc){

    list<employee__c> sq=[select name,empname__c ,Email_Id__c from employee__c where Date_of_Birth__c=today];

    return sq;

    }

    global void execute(Database.BatchableContext bc, List<employee__c> lst)

    {

    list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();

    for(employee__c e: lst)

    {

    Messaging.SingleEmailMessage maileach = new Messaging.SingleEmailMessage();

    list<string> toadd=new list<string>();

    toadd.add(e.Email_Id__c );

    maileach.settoaddresses(toadd);

    maileach.setsubject('birthday wishes');

    maileach.setplaintextbody('happy birthday '+e.empname__c);

    mail.add(maileach);

    }

    Messaging.SendEmailResult[] result =Messaging.sendEmail( mail);

    }

    global void finish(Database.BatchableContext sc)

    {

    system.debug(sc);

    }

    }

    Schedule class

     

    global class BirthdayNameOptions implements Schedulable{

    global void execute(SchedulableContext dc){

    //batch class name

    BatchWishes bth=new BatchWishes ();

    Database.executeBatch(bth,20);

    }

    }

    You can schedule a batch which runs at 12 am every day.Please follow the below steps for Schedule a batch class.

    Go to Setup--->Develop--->Apex classes--->Schedule Apex

    Here you can set the time.Please check the below screenshot.

    Hi,You can achieve the above scenario by using Batch Apex and Schedule Apex. Please check the below sample code.Batch Class global class BatchWishes implements Database.

    Hope this helps you!

    Best Regards,

    Jyothsna