Introduction
We can use an entity list to render the records of an entity from a specific View configured in the entity list. Entity list provides many options like sorting result sets, re-labeling view columns, action buttons, and more. We can configure the details page in the entity list which can be opened to view more details about the record or for edit/updating. In this article, we are going to discuss how we can get record details from the entity list in the Dynamics 365 portal using Liquid.
Requirement
Let’s say we have a requirement to open the details page directly if the entity list only contains one record and just shows the entity list only if we have more than 1 record. We are using the Dynamics Portal trial for the demo purpose. If you want to set up a trial portal, check this link.
We are going to use the Customer Accounts entity list which is available on the trial portal.
Solution
We can implement the above requirement with the help of the web template. In our web template, we can write the logic to retrieve data from the corresponding view (which is used for the entity list) and can check its length like below.
{% entityview logical_name:'account', name:"Customer Accounts" %}
{% assign var_totalaccounts = entityview.total_records %}
In the above code, the logical name is the entity associated with the entity list and the name is the name of the view that we want to render under the entity list in our web page. So in our case, we are displaying the list of the customer accounts. We are using the total_records property to know the total records returned by the view.
Now, we can put a further check on the total accounts. If it is less than 2, we can navigate to the details page for the account. To get columns, we can use the entity view object and use the column's logical name to retrieve it. In our example, we need the id field of the entity record so we can simply get it using entity view. records[0].id.
{% if var_totalaccount < 2 %}
<script>
window.location.href = "/customers/customer-accounts/edit-customer-account/?id={{ entityview.records[0].id }}";
</script>
{% endif %}
Here is the final code for our web template.
{% extends 'layout_1_column' %}
{% block main %}
{% include 'page_copy' %}
{% if user %}
{% include 'entity_list' key:page.adx_entitylist.id %}
{% else %}
<div class='alert alert-block alert-info'>
<span class='fa fa-info-circle'></span>
{% editable snippets 'CustomerService/Support/SignIn' type: 'text', default: resx['CustomerService_Support_PleaseSignIn'], escape: true, tag: 'span' %}
</div>
{% endif %}
{% entityview logical_name:'account', name:"Customer Accounts" %}
{% assign var_totalaccount = entityview.total_records %}
{% if var_totalaccount < 2 %}
<script>
window.location.href="/customers/customer-accounts/edit-customer-account/?id={{ entityview.records[0].id }}";
</script>
{% endif %}
{% endentityview %}
{% endblock %}
Let’s now create a web template by navigating to Portals->Web Templates and using the above code in the code editor.
Once the web template is created, we can create a page template that will be using this web template, like below.
Finally, we need to set our new page template to the page that is rendering the customer accounts entity list, like below.
Now, when you navigate to Customer Accounts, it will directly navigate to the detail page if the Customer Accounts view has only one record. Otherwise, it will stay on the entity list page to show customer accounts.
Hope it will help someone !!