Comparing Dates In Dynamics 365 Portals Using Liquid

Introduction

In this article, we are going to discuss how we can compare two dates using liquid in the Dynamics 365 portal web template.

If you are new to Dynamics 365 portals, Please refer our earlier article for setting up Dynamics 365 trial and you can refer to this KB to provision portal in your trial.

Requirement

Let’s say we have one customer appointment entity where we have an appointment record for the customer. We want to check if the customer appointment date is a future date or not so that we can do further validation on the portal web page.

Solution

First, we are going to use FetchXML in our Web Template where will get customer appointment record based on the query string like the following.
  1. {% comment %} Get Customer appointment based on the query string parameter{% endcomment %}  
  2. {% fetchxml customerappointments %}  
  3. <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  
  4.   <entity name="him_customerappointment">  
  5.     <attribute name="him_customerappointmentid" />  
  6.     <attribute name="him_name" />  
  7.     <attribute name="him_customer" />  
  8.     <attribute name="him_appointmentdate" />  
  9.     <filter type="and">  
  10.       <condition attribute="him_customer" operator="eq" uitype="account" value="{{request.params['id']}}" />  
  11.     </filter>  
  12.   </entity>  
  13. </fetch>  
  14. {% endfetchxml %}  

The above code will get service appointment record for us based on the customer id from the query string parameter. Now we need to get an appointment date from the FetchXML result. While getting an appointment date we will be formatting date using “%y%M%d” combination, you can see liquid date format options here. We will also convert the value to int for easy comparison. You can check the complete list here for working with the different data type filters. While working with the above format I faced an issue for the padding 0, so in case the day is < 10 it was returning single number, for example if today is 2, it returned 2 instead of 02, to get a two digit day and month I used "%y%0M%0d" option if both month and day are <10. So to get today's days with two digit numbers I used the following code.

  1. {% assign var_todaymonth = now | date: "%M"  | integer %}  
  2.  {% assign var_todayday = now | date: "%d"  | integer %}  
  3.    
  4.  {% if var_todayday < 10 and var_todaymonth < 10 %}  
  5.      {%  assign var_today = now | date: "%y%0M%0d" | integer %}  
  6.    
  7.  {% elsif var_todayday < 10 and var_todaymonth >= 10 %}  
  8.    {%  assign var_today = now | date: "%y%M%0d" | integer %}  
  9.    
  10.  {% elsif var_todayday >= 10 and var_todaymonth < 10 %}  
  11.    {%  assign var_today = now | date: "%y%0M%d" | integer %}  
  12.   
  13. {% else %}  
  14.    {%  assign var_today = now | date: "%y%M%d" | integer %}  
  15.   
  16.  {% endif %}  

Further, the above code can be used to get a customer appointment date as well in the same format and finally we can use the following code to compare the date with the current date to check if the appointment date is in the future or not.

  1. {% if customerappointments.results.entities.size > 0 %}  
  2.     {% assign customerappointmentsResult = customerappointments.results.entities[0] %}  
  3.        
  4.     {%  assign var_appointmentMonth = customerappointmentsResult.him_appointmentdate | date: "%M" | integer %}  
  5.     {% assign var_appointmentDay = customerappointmentsResult.him_appointmentdate | date: "%d"  | integer %}  
  6.    
  7.  {% if var_appointmentDay < 10 and var_appointmentMonth < 10 %}  
  8.    
  9.      {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%0d" | integer %}  
  10.    
  11.  {% elsif var_appointmentDay < 10 and var_appointmentMonth >= 10 %}  
  12.      
  13.    {%  assign var_appointmentDate =  customerappointmentsResult.him_appointmentdate | date: "%y%M%0d" | integer %}  
  14.    
  15.  {% elsif var_appointmentDay >= 10 and var_appointmentMonth < 10 %}  
  16.     
  17.    {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%d" | integer %}  
  18.      
  19. {% else %}  
  20.    
  21.    {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%M%d" | integer %}  
  22.    
  23. {% endif %}  
  24.       
  25.    
  26.     {% if var_appointmentDate > var_today %}  
  27.         {% assign var_isappointmentDateInfuture = 1 %}  
  28.     {% else %}  
  29.         {% assign var_isappointmentDateInfuture = 0 %}  
  30.    {% endif %}  
  31. {% endif %}  
I hope it will help someone!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.