Getting Record ID From EntityView In Dynamics 365 Portal

We can use entity list to render records of entity from specific view configured in entity list. Entity list provide many options like sorting result set, re-labelling view columns, action buttons and more. We can configure details page in 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 entity list in Dynamics 365 portal using liquid.

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 set, re-labeling view columns, action buttons and more. We can configure details page in 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 entity list in Dynamics 365 portal using liquid.

Requirement

Let’s say we have a requirement to open details page directly if entity list only contains one record and just shows entity list only if we have more then 1 record. We are using Dynamics Portal trial for the demo purpose. If you want to set up a trial portal, check this link.

We are going to use Customer Accounts entity list which is available on the trial portal.
 
SharePoint

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.
  1. {% entityview logical_name:'account', name:"Customer Accounts" %}  
  2. {% assign var_totalaccounts = entityview.total_records %}  

In the above code, logical name is the entity associated to the entity list and name is the name of the view that we want to render under entity list in our web page. So in our case, we are displaying the list of the customer accounts. We are using total_records property to know 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 entityview object and use column logical name to retrieve it. In our example, we need id field of the entity record so we can simply get it using entityview.records[0].id.

  1. {% if var_totalaccount < 2 %}  
  2. <script>  
  3.    window.location.href="/customers/customer-accounts/edit-customer-account/?id={{ entityview.records[0].id }}";  
  4. </script>  
  5. {% endif %}  
Here is the final code for our web template.
  1. {% extends 'layout_1_column' %}  
  2. {% block main %}  
  3. {% include 'page_copy' %}  
  4. {% if user %}  
  5. {% include 'entity_list' key:page.adx_entitylist.id %}  
  6. {% else %}  
  7. <div class='alert alert-block alert-info'>  
  8. <span class='fa fa-info-circle'></span>{% editable snippets 'CustomerService/Support/SignIn' type: 'text'default: resx['CustomerService_Support_PleaseSignIn'], escape: true, tag: 'span' %}  
  9. </div>  
  10. {% endif %}  
  11. {% entityview logical_name:'account', name:"Customer Accounts" %}  
  12. {% assign var_totalaccount = entityview.total_records %}  
  13. {% if var_totalaccount < 2 %}  
  14. <script>  
  15.    window.location.href="/customers/customer-accounts/edit-customer-account/?id={{ entityview.records[0].id }}";  
  16. </script>  
  17. {% endif %}  
  18. {% endentityview %}  
  19. {% endblock %}  
Let’s now create a web template by navigating to Portals->Web Templates and use the above code in code editor.

Dynamics 365
 
Once web template is created, we can create a page template which will be using this web template, like below.

Dynamics 365
Finally, we need to set our new page template to the page which is rendering customer accounts entity list, like below.

Dynamics 365

Now, when you navigate to Customer Accounts, it will directly navigate to the detail page if 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 !!