Xrm.Utility.lookupObjects to add records to a subgrid

Introduction

This post explains how to ‘re-wire’ the ‘Add Existing Court Orders’ button (Figure 1) to call a custom TypeScript function setDefaultView(). Within this function, the Xrm.Utility.lookupObjects client API is called. Figure 2 shows the ‘Lookup Records’ dialog which is generated when Xrm.Utility.lookupObjects is executed

Figure 1
Figure 2

In detail

The custom TypeScript function setDefaultView() defines the parameters which are sent to Xrm.Utility.lookupObjects() (Figure 3)

Figure 3

Referring to the example in Figure 2, when the ‘Test Court Order 1’ record is selected, it is represented as an entity reference.

entityType = "cpl_courtorder"
id = "{4936E866-F3AE-E11B-DD51-00E3A6AB3E9F}" 
name = "Test Court Order 1"

The TypeScript function associateAddExistingResults() displayed in Figure 4 would then add this record (i.e. the entity reference stored in the ‘results’ field) to the subgrid (the code for doing this isn’t listed in this post).

Figure 4

The function setDefaultView() is added to the court order ribbon (Figures 5 & 6)

Figure 5
Figure 6

The TypeScript function (i.e. the visibilityRuleAddNewCourtOrderButton() in Figure 7) is required so to determine the visibility of the ‘Add Exisiting Court Orders’ button

Figure 7

This function (visibilityRuleAddNewCourtOrderButton()) is added to the court order ribbon

Figure 8

References

https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-utility/lookupobjects