Processing Selected Records using Client-Side JavaScript


When working with a list of records, the “When a Row is Selected” Common Data Service connector allowed for a user to select one or more records to be operated on by an Online flow. This connector is currently labeled as Legacy, and will possibly be deprecated in the future once a replacement has been developed.

Alternatively, updates to records can be handles using Ribbon Buttons, and Client Site JavaScript. This approach can also provide additional flexibility in allowing records to be processed as a group, instead of individually.

This following example shows the process of updating a field on Work Order Products that are shown on a Sub-Grid on a Work Order form.

Creating a JavaScript Library and Function

The first step is to create a JavaScript Web Resource, with a function, that will be used to pass through details for the selected records.

The following temporary function is created to test if record details are being passed successfully and we can see the exact structure of the products data array.


function updateWOPs(products) {
      console.log(products);
}


Create a Ribbon Button

There are a few ways to add and change ribbon butons, but the simplest is using the Ribbon Workbench tool in XRM Toolbox.

First, create a temporary solution, and add the Table for the records that will be selected.


When adding, make sure that no other complaints are added

Now load the Solution in Ribbon Workbench

In the Solutions Elements section, create a new Command.




Press Add Action, and select JavaScript Action, then select the JavasScript library created earlier, and enter the name of the function. Then press the Add Parameter button, select CRM Parameter, and choose SelectedControlsSelectedItensIds.



Note that there are several other useful Parameters that can also be added here, such as “FirstPrimaryItemId”, which in our example, will pass through the GUID of the Work Order.

In the Enable Rules section, press the Add Enable Rule button, and select a New Rule. Press the Add Step button and choose Selected Count Rule. Enter 1 for the Minimum Field. This will cause the Ribbon button to only show if there are one or more records selected.



There are three different rows of button at the top of the window, titled Home, Sub Grid, or Form. Home is used for lists of records, and Sub Grid is used for lists embedded into a form.

Drag a “Button” form the Toolbox and place it to the required button group.


Select the button, and enter in all required properties, including the previously created Command.


Once this is done, press publish. This may take a few minutes to complete. Afterwards, open the list of parent form, select some rows, and the new button should appear.


After pressing the new button, the GUIDs of the select items should will be passed to the JavaScript function, which should print them on the browsers Console.


Processing Results

The results cane now be handled in various ways, such as calling an external Flow for each individual record, or passing all records as a single collection.

For this example, we will make a simple update to each record through the Web API interface. Here is the updated JavaScript function:



function updateWOPs(products) {
    'use strict';
    if (products.length > 0) {
        var subgrid = Xrm.Page.ui.controls.get("workorderproductsgrid");
        var record = {};
        record.msdyn_linestatus = 690970001; // Used
        products.forEach((product) => {
           Xrm.WebApi.online.updateRecord("msdyn_workorderproduct", product, record).then(
                function success(result) {
                },
                function (error) {
                   Xrm.Navigation.openAlertDialog(error.message);
                }
            );
        });
        subgrid.refresh();
    }
}

Extra Notes

  • Records that are passed from a list of sub-grids are limited to the records that are displayed on the form, and don’t include any records on other pages. This is true even when clicking ‘selects all’ for the list. So, for example, If there are 15 records, but a sub-grid is set to only show 10 records on two pages, then a maximum of 10 records can be selected and passed to the JavaScript function.

  • The are several other parameters that can be passed to the JavaScript function, such as “SelectedControlAllItemIds” and “SelectedControlAllItemCount”, but these will also be limited to the records shown on the current page. To get a true count of all the related records, they will need to be retrieved from the JavaScript function.

2 views0 comments

Recent Posts

See All

If you can't see your custom ribbon button icons after upgrading to Unified Interface, follow the steps below to get them back! Icons need to be in svg format to be rendered in Unified Interface. You