Pass the currently selected grid row(s) to a Custom JavaScript Function

Challenge:

How do I create a custom JavaScript action on a Sub Grid that uses the currently selected row in the sub grid to open another window?

Solution:

The CRM2011 Ribbon has a special set of parameters called 'CrmParameters' that provide information about the current session such as the selected rows in a sub-grid. We can use the 'SelectedControlSelectedItemReferences' parameter to pass an array of selected items in the grid to our custom JavaScript function. Each object in the array contains the following fields:

Field Name Type Example
Id string {2D080FA6-8D18-E211-9DB7-000C299FFE7D}
Name string Some Account
TypeCode Number 1
TypeName string account

The following example shows creating a button on the Accounts sub-grid that is enabled if only a single record is selected. Upon clicking, the button, the JavaScript function is passed the selected record information and can then open a new window passing this information as required.


1) Create an solution containing the Account entity
2) Add a new JavaScript Web resource and 16x16 and 32x32 images for your button.
3) In the JavaScript web resource you could add a function to open your webpage. Here is just a simple function to show how to get the selected record information:
function run(selectedItems)
{
    var selectedItem = selectedItems[0];
    // Here you can use the information below to open a new window or whatever!
    alert("Id=" + selectedItem.Id + "\nName=" + selectedItem.Name + "\nTypeCode=" + selectedItem.TypeCode.toString() + "\nTypeName=" + selectedItem.TypeName);
}

4) Open the Solution in the Ribbon Workbench and select the ‘SubGrid’ ribbon using the drop down in the top right of the window.
5) Drag a button to the ‘actions’ group on the design surface, and give it a name such as ‘Run’. You can also set your images using the image web resources added to the solution.
6) Select ‘Commands’ in the Solution elements panel and right-click ‘Add New’
7) Expand the node and select the new Command.
8) In the properties Pane, click the lookup against the Actions, and click ‘Add’
9) Select a ‘JavaScript Function’ and set the function to ‘run’ and the library to be the JavaScript file you added above to the solution. If the web resource does not appear in the lookup, make sure you have added it to the solution that you loaded.
10) Add a new CrmParameter of type ‘SelectedControlSelectedItemReferences’
11) Reselect the button and in the properties set the Command to be the command you have just created.
12) On the Command, Right-Click ->Edit Enable Rules
13) Click ‘Add New’
14) Add a ‘Selection Count Rule’ and set the Minimum and Maximum to 1, and AppliedTo=SelectedEntity. This will ensure that your button is only enabled if a single record is selected.

Your finished customisations will look something like:
Adding SelectedControlSelectedItemReferences parameters

15) Click Publish.
16) Open up an Account, and navigate to Sub-Accounts, create some sub-accounts and you’ll see that your button is only enabled when one record is selected.

CRM subgrid with button

When the Button is clicked it gives something like:

Resulting dialog box

If you needed to pass multiple selected items to the row you could adjust the 'Minimum' and 'Maximum' on the Enable Rule and use all the references in the array (rather than just the first in this example).

Feedback and Knowledge Base