Get a "time of day greeting" for email templates

Sometimes you need to send emails from your CRM that include a greeting such as "Good morning" or "Good evening".

You could just hardcode that into the email template but what if the schedule of that email is different for different customers or might just get moved around sometimes!?

You need it to be dynamic. Additionally, most countries in Europe have DST, you need to handle for that else get a nasty surprise in your emails when the clocks change.

You're typically thinking "I'll just make it as a formula field" but then you would need it in all different sorts of objects depending on your email requirements. Or you might think "I can do that in flow".

Ok flow it is but lets make it extra awesome!

Start off by making an auto launched flow (no trigger) and let's call it Subflow: Calculate Time of Day from Input Date/Time

You then need to create the following resources in the order listed;


1.Input_DateTime

variable date/time input = true

2.LastSundayMarch (Num,0)

31 - MOD(DATE(YEAR(TODAY()), 3, 31) - DATE(1900, 1, 7), 7)

3.LastSundayOctober (Num,0)

31 - MOD(DATE(YEAR(TODAY()), 10, 31) - DATE(1900, 1, 7), 7)

4.IsDST (Boolean)

AND(
    TODAY() >= DATE(YEAR(TODAY()), 3, {!LastSundayMarch}),
    TODAY() < DATE(YEAR(TODAY()), 10, {!LastSundayOctober})
)

5.CurrentHour (Num,0)

VALUE(MID(TEXT(
    IF(
        {!IsDST},
        {!Input_DateTime} + 1/24,  /* Add 1 hour for DST */
        {!Input_DateTime}              /* Use ST*/
    )
), 12, 2))

6.Calculate_MorningAfternoonEvening (Text)

IF(
        {!CurrentHour} < 12,
        "morning",
        IF(
            {!CurrentHour} < 18,
            "afternoon",
            "evening"
        )
    )

7.Output_MorningAfternoonEvening

variable text output = true

Now this may shock you but this is a painfully simple flow, with a single assignment

Give it a try in debug and see what you get...

note that the input time can be anything you populate the input variable with, be it the system time, user time, or even "customer time" 

Because this is a subflow you now have a string value to use elsewhere in another main flow. You can use it as a merge field value by simply replacing a custom merge tag in a larger string with the value of the subflow output like in the below.

Or if you prefer you can write the value to a field on the record > use it as a merge field value in a classic email template > either leave of clear down the value afterwards. However this is a bit system heavy hence I process the merge fields inside the flow and make use of the "Send Better Email" action to help facilitate this.


FAQ

1. Does this work if my system and users have different time zones - yes
2. Does this work for other countries - yes this will correctly observe DST for the below countries only

  • France
  • Germany
  • Spain
  • Italy
  • Netherlands
  • Portugal
  • Poland
  • Sweden
  • Belgium

Non-EU countries that observe DST:

  • United Kingdom
  • Norway
  • Switzerland
  • Serbia
  • Ukraine

3. Can this work for the USA? - yes but you need to change two of your variables!

LastSundayMarch change to SecondSundayMarch

CASE(
    MOD(DATE(YEAR(TODAY()), 3, 1) - DATE(1985, 6, 2), 7),
    0, 8,  /* If March 1st is a Sunday */
    1, 7,  /* If March 1st is a Monday */
    2, 6,  /* If March 1st is a Tuesday */
    3, 5,  /* If March 1st is a Wednesday */
    4, 4,  /* If March 1st is a Thursday */
    5, 3,  /* If March 1st is a Friday */
    6, 9   /* If March 1st is a Saturday */
)

LastSundayOctober change to FirstSundayNovember

CASE(
    MOD(DATE(YEAR(TODAY()), 11, 1) - DATE(1985, 6, 3), 7),
    0, 7,  /* If November 1st is a Monday */
    1, 6,  /* If November 1st is a Tuesday */
    2, 5,  /* If November 1st is a Wednesday */
    3, 4,  /* If November 1st is a Thursday */
    4, 3,  /* If November 1st is a Friday */
    5, 2,  /* If November 1st is a Saturday */
    6, 1   /* If November 1st is a Sunday */
)

Update your other formulas accordingly - your process now handles DST for the USA.

It is possible if you add in the User Locale or same information such as "Contact Location" to blend the EU and US as well as other possible calculation models to give accurate results regardless of the DST format that country(s) uses.

This way when your process runs at 00:00 local time, you can email your global audience and greet them with "morning", "afternoon", or "evening" respective of the time where they are when they are recieving the email!

Example:

capture the Contact Locale eg USA, United Kingdom, France...
Use that Input Variable to decide which set of DST rules to use in the calculation - use the same formats and explanations in this post to create any other scenarios that you may need.