We know that in Dynamics 365 we can display child data by adding a SubGrid into form. So, what actually SubGrid record contains, its nothing but all the related records which has a referenced of the parent entity where we are basically adding the SubGrid. So, the rule is to add SubGrid in a form we should have relationship 1: N, where N is basically related records.
But what happen if you don’t have a direct 1: N relationship but you want to display child data based on some lookup value available in the forms. let me give you a scenario, we have a custom entity called “Subscription” where we have an Account lookup field available in subscription entity. So, there is no direct relationship between subscription and contacts entity. But we know the relationship between Account and contact in Dynamics 365. An account can have multiple child contacts. So, coming back to our scenario, our customer wants to display all account related contacts in the subscription’s forms based on the Account lookup value sleeted in the Subscriptions forms.
You might be thinking to create an web resource and fetch the related contacts records to display in Subscription entity forms. Or there is another way using grid setFilterXml properties.
formContext.getControl(“contacts”).setFilterXml(fetchXml);
Have you ever thought about Dynamics 365 quick view forms? A quick view control on a custom business app form displays data from a record that is selected in a lookup on the form. Which means quick view forms not only display the data, it’s a form where you can include subgrid as well to display in another entity forms.
So, the idea is to overcome this scenario we will create a quick view form in Account entity where will basically include a subgrid of related contacts.
Here in my solution you can see I have three entities. Account, Contact and Subscriptions.
You will be land to a quick view form design window where I will add SubGrid of contacts, click on Insert–> SubGrid.
Choose Contacts related to Account, click on ok.
Here you can see SubGrid has been added in the quick view forms .
You can change the form name, here you can see my quick view form is ready.
Rename the “Tab” to “Contacts” to understand the what it is for.
Let me remind you that our main purpose to display contacts of associated with Account lookup in Subscriptions entity. Here is Subscription entity forms where I have an Account lookup field.
Look here If I want to include Contacts subgrid I cannot see anything as there is no relationship between subscription and contact entity so its expected .
Let’s now add the quick view form into subscriptions forms. To do that we have to click on “Quick View Form” from the form navigation.
Here make sure we need to choose the lookup field in which on change we will be filtering the subgrid , after select the lookup field you can see all the available quick view forms of the associated lookup entity. Here I select the Quick View Form “New Form” which I created above. Click on ok.
Above screenshot we can see quick view forms added in the Subscription entity forms. Now do save and publish.
Now when I am going to create one new subscription record ,I choose account lookup I can see all related contacts in the quick view forms.
I changed Account lookup with a different value, now see contact SubGrid also filtered based on parent account lookup.
New Subscription Create form-
Once I created subscription , I can see the similar behavior , subgrid data is populating based on the account lookup selected value.
Hope this helps!!!.
*This post is locked for comments