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 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 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 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 %}
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 “%M%d%y” combination, you can see liquid date format options here. Liquid treats the date as string data type, so we will also convert the value to int for easy comparison. You can check complete list here for working with the different data type filters.
{% assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%M%d%y" | integer %}
Further, 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_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%M%d%y" | integer %}
{% assign var_today = now | date: "%M%d%y" | integer %}
{% if var_appointmentDate > var_today %}
{% assign var_isappointmentDateInfuture = 1 %}
{% else %}
{% assign var_isappointmentDateInfuture = 0 %}
{% endif %}
{% endif %}
Hope it will help someone !!

Like
Report
*This post is locked for comments