We are going to use 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, 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.
- {% 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 use the above code in code editor.
Once web template is created, we can create a page template which will be using this web template, like below.
Finally, we need to set our new page template to the page which is rendering customer accounts entity list, like below.
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 !!