Advertisements

Archive

Posts Tagged ‘Liquid’

Dynamics Portals – Execute FetchXML and Parse the results – Liquid script

January 22, 2018 Leave a comment

In one of our Portal requirements, we had to build custom screen (i.e., Web Template) and retrieve the results from Dynamics 365 and bind to grid.

In this article, I am going to detail the steps to Execute FetchXML based on conditions and read the results.

As we know that Dynamics portals use Liquid to add dynamic content to pages, lets prepare the Liquid.

High level design:

  • Liquid executes only on the page load, hence create a new Web Template (will be called only in background) and add the Liquid in it.
    • Imp Points:
      • In the web Template where you copy your Liquid to, specify the ‘MIME Type’ as ‘application/json’WT_Resolved Cases
        • You need to uncheck, below property in the ‘Page Template’ associated with the Liquid ‘Web Template’.

WT_1

  • From my Search screen web template, on change of Dropdown, I will call the above Web Template by passing selected the view as Query string.
  • Up on calling the background template, it executes the Liquid and return the results in Json collection
  • Capture the collection and render the results in table.

Liquid to fetch Cases:

In below liquid, I am reading the query string parameter ‘V‘.

  • If ‘V’=0, Prepare ‘Open Case’ filter
  • If ‘V’=1, Prepare ‘Resolved Case’ filter
{% fetchxml cases %}
<fetch>
<entity name=”incident”>
<attribute name=”incidentid”/> <attribute name=”title”/> <attribute name=”createdon”/> <attribute name=”casetypecode”/> <attribute name=”ticketnumber”/> <attribute name=”subjectid”/> <attribute name=”caseorigincode”/> <attribute name=”customerid”/> <attribute name=”statuscode”/> <attribute name=”statecode”/> <order descending=”true” attribute=”createdon”/>
<filter type=”and”>
{% if request.params[‘V‘] == 0 %}
<condition attribute=”statecode” value=”0″ operator=”eq”/>
{% endif %}
{% if request.params[‘V’] == 1 %}
<condition attribute=”statecode” value=”1″ operator=”eq”/>
{% endif %}
</filter>
</entity>
</fetch>
{% endfetchxml %}
{ “totalcount”: {{ cases.results.total_record_count }},
“morerecords”: {{ cases.results.more_records }},
“page”: {{ request.params[‘page’] | default: 0 }},
“results”:
[
{% for item in cases.results.entities %} {
“incidentid”: “{{ item.incidentid }}”,
“title”: “{{ item.title }}”,
“statuscode”:”{{ item.statuscode.label }}”,
“createdon”:”{{ item.createdon }}”,
“subjectid”:”{{ item.subjectid.name }}”,
“customerid”:”{{ item.customerid.name }}”
}{% unless forloop.last %},{% endunless %} {% endfor %}
] }

Liquid’s Field Parsing Syntax:

If you notice, I have used {% for item in cases.results.entities %} to loop through the FetchXML results.

  • To read a Look up field text, {{ item.customerid.name }}
  • To read a Option set Label, {{ item.statuscode.label }}

🙂

Advertisements