Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

19 March 2012

How to pass parameters from subgrids to JavaScript

In this post I will show you how to pass parameters regarding a grid or subgrid to a JavaScript function by clicking a custom button on the ribbon menu.
A common requirement is to pass poarameters to a JavaScript function called when clicking a Ribbon button.
To pass parameters regarding a grid or a subgrid we can use the "crmparameter" element of the RibbonDiffXml file.
The available parameters can be devided in three groups:

  1. Selected items 
    • SelectedControlSelectedItemCount: The number of selected items in a grid or subgrid.
    • SelectedControlSelectedItemIds: A string array of GUID Id values for all selected items in a grid.
    • SelectedControlSelectedItemReferences: An array of EntityReference objects that represent all the selected items in the grid
  2. All items
    • SelectedControlAllItemCount: The number of items in a grid or subgrid.
    • SelectedControlAllItemIds: A string array of GUID Id values for all items in a grid.
    • SelectedControlAllItemReferences: An array of EntityReference objects that represent all the items in the grid.
  3.  Unselected items
    • SelectedControlUnselectedItemCount: The number of unselected items in a grid or subgrid.
    • SelectedControlUnselectedItemIds: A string array of GUID Id values for all unselected items in a grid.
    • SelectedControlUnselectedItemReferences: An array of EntityReference objects that represent all the unselected items in the grid. 
The way you can pass these parameters by clicking a Ribbon button is shown below:

  <CommandDefinition Id="Mscrm.SubGrid.opportunity.Command">
            <EnableRules>
              <EnableRule Id="Mscrm.SubGrid.opportunity.EnableRule"></EnableRule>
            </EnableRules>
            <DisplayRules></DisplayRules>
            <Actions>
              <JavaScriptFunction Library="$webresource:functions.js" FunctionName="MyFunction" >
                <CrmParameter Value="SelectedControlSelectedItemIds" />
                <CrmParameter Value="SelectedControlSelectedItemCount" />
              </JavaScriptFunction>
            </Actions>
</CommandDefinition>

To access these parameters add them to your function definition like below:

function MyFunction(SelectedControlSelectedItemIds, SelectedControlSelectedItemCount) {
      for (i = 0; i < SelectedControlSelectedItemCount; i++) {
       }
}


So as you can see it is possible to access records displayed in a subgrid using supported code, so don't get tempted to use document.getElementById.  ;)

I hope this help.



4 comments:

  1. Hi

    Interesting post.

    I was wondering by using some of this code if it would be possible to essentially create a complete copy of a quote for example, this would include retrieving products from a sub grid, passing them over to a new form and putting them back into a sub grid?

    Thanks

    ReplyDelete
    Replies
    1. If I understand your question. That should be all possible.
      You should use the "SelectedControlSelectedItemIds" parameter,
      which gives you a string array of GUID Id values for all selected items in the sub-grid.
      You can then use them to retrieve the product records and use the details you need to create the quote records.
      The products related to the quote can then be displayed in a new sub-grid.
      I hope I answered your question.

      Delete
  2. I was trying to use the same funda, but alas it did not work for main page. Can you please confirm if this works ?

    ReplyDelete
    Replies
    1. Hi the same should work for the main page grid "HomePageGrid".

      Delete