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.



7 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
  3. Hello, I have a question, let me put you in context:

    We have an entity called ¨Chronogram Event¨ that has a 1:N relationship to a child entity that it is called ¨Project Service Event¨.

    In the parent entity we have several subgrids that lead to the same entity, but they have differente names according to the forms available in the child entity. However, the users are required to select the form manually, and sometimes it causes confusion among the users, so our requirement is to be able to swap the form depending on the subgrid the user selected on the parent record (remember that we have several subgrids with different name but they are the same entity, but it has different forms).

    Do you know if this is possible to do it with javascript using the functions you explained above or is there any other option?

    If you could help me to clarify if this can be done I will highly appreciate it.

    Thank you in advance.

    ReplyDelete
    Replies
    1. Hi, I think that is achievable. You would have to add a button on your parent entity ribbon (the button could be disabled when no subgrid is selected). When the user clicks on the button you'd have to retrieve the selected subgrid by using the selectedcontrol parameter in the ribbondiff definition, from there you should be able to get the name of the subgrid and determine which form should be loaded.
      I hope this helps!
      Luciano

      Delete
    2. Thanks Luciano, we will give it a try, I´ll let you know if that worked. Regards.

      Delete