Dynamics Portals – Execute FetchXML and Parse the results – Liquid script
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’
- You need to uncheck, below property in the ‘Page Template’ associated with the Liquid ‘Web Template’.
- In the web Template where you copy your Liquid to, specify the ‘MIME Type’ as ‘application/json’
- Imp Points:
- 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 }}
How to execute the FetchXML:
- Once you have your FetchXML placed in the ‘Web Template’ and map the ‘Web Template’ to a ‘Web Page’, below is the script snippet to execute FetchXML:
var yourWebPageUrl = “/WebPagePartialUrl?querystring“;
var xhr = new XMLHttpRequest();
xhr.open(‘GET’, yourWebPageUrl, true);
xhr.responseType = ‘json’;
xhr.onload = function () {var status = xhr.status;
if (status == 200) {
try {
// Parse the ‘Result’ Json collection
var stringyObj = JSON.stringify(xhr.response);
sandblastURL = JSON.parse(stringyObj).results[0].setting;
} catch (e) {}
xhr.send();
}
🙂