Send Birthday Email to Contacts

Send Birthday Email to Contacts :

Generally will get the requirement from client to send Birthday emails to their customers. Everyone will suggest going with Console Application schedule to run at the specific time. But is there a way to do it using OOB features?. Yes, we can achieve this through OOB features simply by using JS + Workflows and also it is a very simple approach.

Components Used: 1. Custom Entity(Scheduler) with N:1 relationship with Customer and add Date field as Birthday.

Below are the steps to achieve the scenario:

Step1: Create a Date Only field on contact entity and place it on the form, named “Custom Birthday” and Create another Date only field on contact entity and place it on the form, named “Today’s date”. Make this two fields hide in the form.

Custom Fields form Display.png

Step2: Birthday Field on the change set the values to Custom Birthday and Today Date fields. Through Js will updates the values to this fields. If any customer record is created throgh plugin code, then set values to this dummy fields. Dummy field dates should as follows.

Eg. Birthday = 21 10 1993 and Custom Birthday = 21 10 2018( Same Day, Month, Current Year) and Today Date = 23 05 2018.

function SetBirthday() {
debugger;
var Birthday = Xrm.Page.getAttribute(“birthdate”);

 

if(Birthday != null && Birthday != undefined) {
var dob = new Date(Birthday.getValue());
var actualday = dob.getDate();
var actualmonth = dob.getMonth();
var currentyear = (new Date()).getFullYear();
var custombirthday = new Date(currentyear, actualmonth, actualday);
Xrm.Page.getAttribute(“gbs_custombirthday”).setValue(custombirthday);

 

var todayDate = new Date();
var td = todayDate.getDate();
var tm = todayDate.getMonth();
var ty = todayDate.getFullYear();
Xrm.Page.getAttribute(“gbs_todaydate”).setValue(new Date(ty, tm, td));
}
}

On change Set Birthday Func.png

Step3: Create New Entity named as “BirthdayEmailSchedular“. Create date field with as “ScheduledBirthday” and create one more lookup field with contact entity with the name as “contact“. Add this two fields to the form as below.

Birthday Email schdeular.png

Step4: Creating Birthday Email scheduler records to send email on the birthday through the workflow. When contact is created or updated then has to create birthday email scheduler records to send email on birthday. Create workflow on contact entity and trigger on Create of contact and also on change of the custombirthday field with the workflow steps.

Creating Birthday Email schdeular Records Workflowa. First Condition if Today date is greater than Custom Birthday means already birthday completed and has to add 12 months to the custom birthday.Birthday Email schdeular.png

b.Second condition if today date is less than custom birthday means birthday yet to come. So we have to create Birthday schedule.

C. Third condition. if today date equals to custom birthday means today is customers birthday, so has to send email to the customer.Birthday Email schedule workflow 3.png

Step5: Bulk Record Deletion

Every Contact is created then through workflow, birthday email scheduler records will be created with scheduledbirthday date. If scheduledbirthday equals to today then mail has to send to the customer and scheduled birthday record has to create with 12months for next birthday and today scheduled record has to delete. Through bulk record deletion job if the scheduled day is today, bulk delete job will fire before deleting the record another workflow will fire, it will send the email to the customer and next 12 months schedule record will create.

Bulk deletion job1.pngBulk delete job 2

This bulk delete job will run every day on the time and deletes the scheduler records.

Step6: Create a workflow which should run on BirthdayEmailScheduler entity and run it before the deletion of schedule record.Deletion of Birthday Email Schduele workflow.png

a. If Contact CustomBirthday date equals to the scheduledBirthday of birthday email schdeuler then has to send Email to the customer as well as for next year birthday has to create another schedule record.

Deletion of Birthday Email Schduele workflow3.png

and Finally the process of creation of birthdayemailschedule records and deletion will continue and emails will goes to the customers on their birthdays.

That’s it. Enjoy:-)

Advertisements

Difference in days between two dates

 Difference in days between two dates :

Here, we are stamping the diffrence in days between two dates i.e first date and second date.
function days()
{
debugger;
if (Xrm.Page.getAttribute(“gb_firstdate”).getValue() != null && Xrm.Page.getAttribute(“gb_seconddate”).getValue() != null )
{
debugger;
var days;
var firstdate = Xrm.Page.getAttribute(“gb_firstdate”).getValue();
var seconddate = Xrm.Page.getAttribute(“gb_seconddate”).getValue();

if (firstdate > seconddate)
days = Math.round(((((firstdate – seconddate) / (1000 * 60 * 60 * 24)) * 10) / 10));
else
days = Math.round(((((seconddate – firstdate) / (1000 * 60 * 60 * 24)) * 10) / 10));

if (days != null)
{
//days = days – 1;
Xrm.Page.getAttribute(“gb_days”).setValue(days);
Xrm.Page.getAttribute(“gb_days”).setSubmitMode(“always”);
}

}
}

 

That’s It. Enjoy 🙂