Get FormContext in Sub-grid ribbon event in Dynamics 365 V9.0

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com


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/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


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

6 thoughts on “Get FormContext in Sub-grid ribbon event in Dynamics 365 V9.0”

  1. Suresh Bharadwaj Kala

    Hi Debajit,
    How about getting getFormContext() for button placed on HomePageGrid ? Do you have any suggestion.
    Regards
    K Suresh

    1. Hi Suresh,
      Thanks for reading my blog. For homepagegrid I believe instead of formContext, it gives the gridContext..as formContext is only valid when the record is displayed on a form..Let me know if you are having any issues with finding the gridContext.
      Best Regards
      Debajit

  2. Hi Debajit,
    I think Microsoft has updated this ‘selectedControl’ parameter.
    Now I can have the formContext directly, just from a subgrid ribbon button, to the main entity Case:
    var caseTitle = selectedControl.formContext.getAttribute(“title”).getValue();
    My Dynamics 365 version is Version 1710 (9.1.0.8422) online
    Regards,
    Zhenyu

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading