Getting Form Context from Sub-grid ribbon button in Dynamics V9.0 Gotchas!

After my last blog on getting the formContext and the client URL from a ribbon button on the form in Dynamics Version 9.0, I was simply taken back by the comments and was truly satisfied knowing that it had helped so many blog readers.

https://debajmecrm.com/2018/11/16/xrm-page-is-deprecated-with-version-9-0-how-do-i-get-the-clienturl-and-form-data-in-ribbon-actions/

However one of the questions was on how to get the formContext when a sub-grid button was clicked. So basically the requirement here is when the sub-grid button is clicked, the UI elements on the form needs to be accessed like the tabs, fields, sections, formSelector etc

And now with V9.0, since Xrm.Page has been deprecated, we need to use the formContext to access and any form-related values.

So I decided to give this a try.

I created a custom ribbon button on the account sub-grid and then on the click of the function, I added couple of CrmParameters

<Actions>
              < JavaScriptFunction FunctionName="subgridEvent" Library="$webresource:new_contactformload.js">
                <CrmParameter Value="PrimaryControl" />
                < CrmParameter Value="SelectedControl" />

              </JavaScriptFunction>

</Actions>

One is PrimaryControl and the other is SelectedControl. Well, PrimaryControl was very helpful last time for a form button, since it gave me the much desired FormContext with which I was able to play with the entire form.

Somehow I was reluctant to use the PrimaryControl still I included it.

SelectedControl is very much necessary because I know it gives me the Grid Context which would help me to play with the Grid. And I was pretty sure that it would give me something to access the FormContext.

All set and good. I clicked on the Test Button in the Account sub-grid.

image

While debugging, I got my two parameters – PrimaryControl and SelectedControl.

As I expected PrimaryControl was useless and SelectedControl gave me the Grid Context.

image

I kept on searching for something in the SelectedControl and to  my horror nothing was there to get the FormContext.

I stated thinking otherwise. How about storing the formContext in a global variable when the form loads? Well that does not work either. When you move to associated view, your form refreshes and unfortunately you won’t find that script global variable in there when the sub-grid button is clicked.

So is there no other way? Searched the heck out of google and could not find a permanent solution. Searched in docs. Still no luck.

So back to where I started. No supported way I realized or may be I could not find?

I started digging now into the parameter – PrimaryControl and found a method – get_crmFormControl(). And finally from this I was able to get the formContext.

Code below.

function subgridEvent(primaryControl, selectedControl) {
    var formContext = primaryControl.a.get_crmFormControl().getFormContext();
    var grid = selectedControl.getGrid();

    // interact with the form using formContext now
}

Please note that this is not a documented approach and hence unsupported. However I could not find anything else from this and no where this scenario is mentioned as well.

Would really like to hear if you get a solution working which is supported.

Hope this helps!

Debajit Dutta
(Dynamics MVP)
For consultation/ training visit
http://www.xrmforyou.com or reach out to us at info@xrmforyou.com

Advertisements

2 thoughts on “Getting Form Context from Sub-grid ribbon button in Dynamics V9.0 Gotchas!

  1. Hi Debajit,

    I have the same issue. On the associated view screen, there is no getFormContext on primaryControl. I tried to use your code but it is not available. If you have any update on this, please share. Thanks~

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s