Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)
I have an Apex Scheduler where I want to send emails to my colleagues when there birthday's are approaching. It's like 2 days to their birthdays. So can anyone help me how I can able to achieve that?

1 Answer

0 votes
by (26.7k points)

You can try with the below code this will help to achieve it:

global class BirthdayNameOptions implements Schedulable {

    global void execute (SchedulableContext ctx) {

        sendBirthdayEmail();

    }

    public void sendBirthdayEmail() {

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

        for ( Contact con : [SELECT Id, Name FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2)] ) {

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

            mail.setTemplateId('00XJ0000000M31w');

            mail.setTargetObjectId(con.Id);

            mail.setSaveAsActivity(false);

            mails.add(mail);

        }

        if ( mails.size() > 0 )

            Messaging.sendEmail(mails, false);

    }

}

I hope this will help.

Want to know about Salesforce Apex? Prefer this tutorial on salesforce automation.

Want to become a Salesforce developer? join salesforce developer certification now!!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 5, 2020 in Salesforce by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...