Send Birthday Email To Customers[ Workflow + JavaScript]

No comments

In one of my recent blogs I have explained and showed you an approach to send birthday emails to your customers in dynamics CRM using a code solution in which i have used a console application, you can find it here :  Send Birthday Email To Customers [ Simplest Code Solution]

However In this blog i have come up with an approach of using Workflow + JS. So without further due, lets get into this.

Things to do:

  1. Create a Date Only field on contact entity and place it on the form, named “Custom Birthday” .
  2. Create another Date only field on contact entity and place it on the form , named “Today’s date” [you will know why i have used this later]1
  3. Add below Javascript code to your on change event of Birthday field:
    var SetBirthday = function (executionContext)
    {
     debugger;
     var actualBirthday = executionContext.getFormContext().getAttribute("birthdate");
     
     if(actualBirthday!= null)
     {
    
    var date1 = new Date(actualBirthday.getValue());
     var dd = date1.getDate();
     var mm = date1.getMonth();
     var yyyy = (new Date()).getFullYear();
    
    var customField = executionContext.getFormContext().getAttribute("new_custombirthday");
     customField.setValue(new Date(yyyy, mm, dd));
    
    
    
    
    var todayDate = new Date();
     var d = todayDate.getDate();
     var m = todayDate.getMonth();
     var y = todayDate.getFullYear();
    
    var dateField = executionContext.getFormContext().getAttribute("new_todaysdate");
     dateField.setValue(new Date(y, m, d));
    
    }
    
    }

2

What this javascript will do is as soon as someone enters the birthday of contact – it will extract the day and month part from the birthday and put in the next field with current year. So, for e.g if my birthday is entered as  05-01-1993 , it will set the custom birthday to 05-01-2018 & the next thing it doesn’t is simply put today’s date in the today’s date field.

4. Create a new entity named “Schedular” and add below field to it:
a. Deletion date (Date Only Field)
b. Contact ( lookup to contact entity)
3

 Workflow

  1. Create a workflow and name it as required on contact entity.  Run this on Create of contact and also on change of the birthday field.4
     a. First condition to check if “Today’s Date“(custom field we created) is less than “Custom birthday” field.  If this condition passed we creating a schedular record by putting the custom birthday field as deletion date.

    5
    b. Second condition to check if  “Today’s Date“(custom field we created) is greater than “Custom birthday” field.  If this condition passed we creating a schedular record by putting the custom birthday field + 12 months as deletion date.
    6
    c. third condition simply checks if Today’s date is equal to Custom birthday field – sending email right away.
    7.JPG

    It will create schedular records with required deletion date and send email right away if birthday is today or on the changed date.

    Bulk Delete Job

    1. Create a bulk delete job with condition as below to simply delete schedular records whose deletion date is today , the job should run everyday ( preferably in the morning  as as business needs)
    Go to Settings > Data Management> Bulk record deletion > Create a new job :

    8

    9.JPG

  2. Save and close the job – this will run every day on the time you will select above and delete schedular records.

    Final Workflow

    1. Create a workflow which should run on Schedular entity and run it before the deletion of schedular record ( convert it to real-time mode)

    10

    a. first condition checks if Deletion date of the schedular is equal to contact’s custom birthday date, we are sending birthday wishes. ( because birthday might have changed later and create other schedular records hence we must check if this shedular is the correct one)

    b. within first condition after sending birthday email – we have to create a another workflow schedular record with deletion date + 12 months ( for the next year ).

    11

    c.  add an otherwise condition to check if deletion date is not equal to the custom birthday of the contact. we would not send email in that condition and simply stop the workflow.

    So, the process of deletion of shedular records and creation will continue and send email to customers on their birthdays.

 

Few things:

  1. Instead of using custom today’s date i tried using Execution time of the workflow but because the birthday field is Date only but Execution time is  Date Time format. the equal to condition will never meet and always go in less then condition.
  2. You can hide “Custom birthday” & “Today’s Date” on the form.
  3. The javascript above is very specific to dynamics crm 365 v9. Can be changed for other CRM versions such as instead of using  :var actualBirthday = executionContext.getFormContext().getAttribute(“birthdate”);

    you can use :

    var actualBirthday = Xrm.Page.getAttribute(“birthdate”).getValue();

  4. The schedular option is really helpful and let you escape from waiting condition wofkflows. To read more :  Escape from waiting condition workflows – Microsoft Dynamics CRMI hope this helps!
    please leave your comments for any suggestions or doubt.
Advertisements