How to set Dynamics 365 related entity fields using quickview forms

Challenge

You have a form in Dynamics 365 in which you need to show field values from related entity. It is naturally possible to do this by making a request to the Dynamics server side API’s from client side code. There are a few possibilities to that but I will not focus on those in this blog post.

Instead of server side API requests, there is a simpler way to utilize Dynamics quickview forms and JavaScript. This way you don’t need to do any API requests in code.

The scenario could be such where you have for example a Dynamics 365 Case entity form, and you’d need to show the related Product and Account entity values in lookup fields. These Product and Account lookup fields would get loaded when a Customer Asset entity lookup field value gets changed. This would mean that when user selects a customer asset for a case, then product and customer information would get loaded automatically to the form without user’s having to select them manually from individual lookup fields. By just using quickview form, the fields in it are read-only. So that’s why you’d need to have separate Account and Product lookup fields in Case entity form into which you set the values through JavaScript from the quickview form.

Solution

Below are steps how to do this:

  1. Create one quickview form to the Customer Asset entity. Add two Customer Asset entity fields into this quickview form:
    1. Account (lookup)
    2. Product (lookup)
  2. Insert that quickview form to the Case entity form and set the quickview form properties so that the name of the form is “CustomerAssetProductCustomer” and then set the data source properties of that quickview form according to your field names in the CRM form. You can set the quickview form not be visible by default.
  3. Add a small piece of JavaScript code to the OnChange event of the Customer Asset lookup field on the Case entity form. This JavaScript will fetch the Account and Product values from quickview form and set those values to the corresponding lookup fields on the Case form. While I was testing this scenario, I noticed that without adding a small delay to the JavaScript function processing, the quickview form was not loaded yet when the JavaScript got processed and that’s why it always set incorrect Account and Product values to the lookup fields. Those were the Account and Product lookup values from the previously selected Customer Asset. By adding a half a second delay, the solution started working properly.

function populateFieldsFromCustomerAssetRecord()

{

if(Xrm.Page.getAttribute(“yourcustomerassetlookupfield”).getValue())

{

setTimeout(populateFields, 500);

}

}

 

function populateFields()

{

var quickViewControl = Xrm.Page.ui.quickForms.get(“CustomerAssetProductCustomer”);

if (quickViewControl)

{

if(quickViewControl.isLoaded())

{

var product = quickViewControl.getControl(“msdyn_product”).getAttribute().getValue();

var account = quickViewControl.getControl(“msdyn_account”).getAttribute().getValue();

Xrm.Page.getAttribute(“productid”).setValue(product);

Xrm.Page.getAttribute(“customerid”).setValue(account);

}

}

}


At Cloudriven, we help organizations in every step of the Dynamics 365 projects.  If you require any help just contact us. We are here for you !

Ota yhteyttä