Refresh Quick View form control using client/ XRM API in dynamics 365/ CDS

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
Recently I came across a requirement where the customer needed to refresh the quick view form on an entity on change of the related field programmatically. So let me explain the requirement here.

  • Let’s say we have custom entity – “Quick View Test”.
  • Quick View Test has a  N:1 relationship with contact.
  • Quick view form of the contact is designed as shown in the screen shot below. It has the following stuffs
    • First Name
    • Last Name
    • Email
    • A sub-grid showing Related emails

image

  • The form for the Quick View Test Entity is designed as follow

  image

So far so good. The requirement here is when the user changes the contact lookup on the entity form, the quick view form should refresh and reflect not only the new contact’s first name and last name, but should refresh the associated emails grid as well to show the emails associated to the contact.
Wondering what’s there to worry about? Doesn’t changing the contact lookup automatically trigger refresh of all the controls on the forms?
Well, unfortunately it does not.
I have a contact called Andrew Book with two associated emails.
image
However when I change the contact lookup on the form, the grid in the quick view form does not refresh.
image
The user have to explicitly refresh the grid to view the associated emails.
Well this can be done with a simple script with the help of new Client API’s for Quick View Forms.

// JavaScript source code
function QuickViewFormTest() {
    var quickViewControl = Xrm.Page.ui.quickForms.get("related_contact");
    if (quickViewControl != null) {
      
        setTimeout(function () {
            quickViewControl.refresh();
        }, 500);
    }
} 

Register this function on change of contact and you would see the grid refreshing.
You might be thinking, why there is a timeout of 500 milliseconds. The reason is when you change the contact lookup OOB it fires a refresh of the quick view form which unfortunately does not refresh. Our custom code fires another re-fresh just after that which loads the grid.
If you want to access the individual controls on the Quick View form, you would even do that using the below code.

var emailsGrid = quickViewControl.getControl(“grid_emails”) // grid_emails is the control name
emailsGrid.refresh();

The above code access the grid in the quick view control and refreshes the same.
Hope this helps!
Debajit Dutta
(Microsoft Business Solutions MVP)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading