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.
- {% comment %} Get Customer appointment based on the query string parameter{% endcomment %}
- {% fetchxml customerappointments %}
- <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
- <entity name="him_customerappointment">
- <attribute name="him_customerappointmentid" />
- <attribute name="him_name" />
- <attribute name="him_customer" />
- <attribute name="him_appointmentdate" />
- <filter type="and">
- <condition attribute="him_customer" operator="eq" uitype="account" value="{{request.params['id']}}" />
- </filter>
- </entity>
- </fetch>
- {% 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.
- {% assign var_todaymonth = now | date: "%M" | integer %}
- {% assign var_todayday = now | date: "%d" | integer %}
-
- {% if var_todayday < 10 and var_todaymonth < 10 %}
- {% assign var_today = now | date: "%y%0M%0d" | integer %}
-
- {% elsif var_todayday < 10 and var_todaymonth >= 10 %}
- {% assign var_today = now | date: "%y%M%0d" | integer %}
-
- {% elsif var_todayday >= 10 and var_todaymonth < 10 %}
- {% assign var_today = now | date: "%y%0M%d" | integer %}
-
- {% else %}
- {% assign var_today = now | date: "%y%M%d" | integer %}
-
- {% 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.
- {% if customerappointments.results.entities.size > 0 %}
- {% assign customerappointmentsResult = customerappointments.results.entities[0] %}
-
- {% assign var_appointmentMonth = customerappointmentsResult.him_appointmentdate | date: "%M" | integer %}
- {% assign var_appointmentDay = customerappointmentsResult.him_appointmentdate | date: "%d" | integer %}
-
- {% if var_appointmentDay < 10 and var_appointmentMonth < 10 %}
-
- {% assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%0d" | integer %}
-
- {% elsif var_appointmentDay < 10 and var_appointmentMonth >= 10 %}
-
- {% assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%M%0d" | integer %}
-
- {% elsif var_appointmentDay >= 10 and var_appointmentMonth < 10 %}
-
- {% assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%d" | integer %}
-
- {% else %}
-
- {% assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%M%d" | integer %}
-
- {% endif %}
-
-
- {% if var_appointmentDate > var_today %}
- {% assign var_isappointmentDateInfuture = 1 %}
- {% else %}
- {% assign var_isappointmentDateInfuture = 0 %}
- {% endif %}
- {% endif %}
I hope it will help someone!