15

In Dynamics 365 9.0 there was quite a big change regarding how to access form attributes and controls - instead of Xrm.Page namespace, we should pass executionContext to a function and get formContext using getFormContext() function. This is working fine and I had never a problem with using this approach.

However I did not figured out yet how to properly access formContext in functions that are called from Ribbon. The documentation says that this should be really straightforward:

https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions

function myFunction(executionContext) {
    var formContext = executionContext.getFormContext();
    var focusFieldValue = formContext.ui.controls.get(PrimaryControlId).getAttribute().getValue();
}

But it does not say how to pass executionContext to Ribbon function. In normal functions there is a checkbox "Pass execution context as first parameter" but what about Ribbon functions? There are parameters that we can pass into these functions, but they are simply GUID of selected records, or type of selected record or even a list of objects but I could not find in documentation, if there is a parameter equal to executionContext. Has anybody already solved this problem?

Also I know that I can use Xrm.Page and it will work (for now at least...) but I would like to know, how it can be done using the latest guidelines in version 9.0

Update 1:

As per Scott's suggestion and this article i passed PrimaryControl to my Ribbon command but unfortunately, the argument is of type Mscrm.FormControlLite and it does not have getAttribute function or any function that would allow to access the formContext (at least I don't see anything useful). Some screenshot from Developer tools: enter image description here

So it looks like a form representation of some kind, but is probably not related to formContext (I assume that if a Ribbon would be called from a list of records, this item can be of type of grid or something like that)

Pawel Gradecki
  • 3,476
  • 6
  • 22
  • 37

4 Answers4

15

As per https://learn.microsoft.com/en-us/dynamics365/get-started/whats-new/customer-engagement/important-changes-coming#some-client-apis-are-deprecated you pass it as the PrimaryControl parameter.

enter image description here

So if you pass the PrimaryControl as the second parameter to a command function like this you can use

arguments[1].getAttribute(…)
Pawel Gradecki
  • 3,476
  • 6
  • 22
  • 37
Scott Durow
  • 557
  • 4
  • 10
  • Thanks Scott for reaching out! Unfortunately when I pass PrimaryControl it is of type `Mscrm.FormControlLite` and it does not have `getAttribute` method or anything I can see that can be used to replace `Xrm.Page` (I updated my question). Based on the article you posted it should work this way, so that's strange... – Pawel Gradecki Jan 11 '18 at 12:06
  • Are you calling a command from a form or grid button? In a form context inside the Unfiied Client I've used this technique successfuly in Version 1612 (9.0.0.3172) (DB 9.0.0.3172) online – Scott Durow Jan 11 '18 at 12:22
  • Unified Interfce is the key word here :) You are right, it works in UI, I hope that they will make it also work in normal web client... – Pawel Gradecki Jan 11 '18 at 12:41
  • 2
    Unfortunately, there are still many differences between the Web and the UUI - maybe use something like var formContext = primaryControl.ui ? primaryControl : Xrm.Page – Scott Durow Jan 11 '18 at 13:08
  • Yes, I eventually ended up doing so, thanks for the help! – Pawel Gradecki Jan 12 '18 at 07:36
  • @ScottDurow - curious of your thoughts. To me, it seems *anywhere* that `Xrm.Page` is used, that text can confidently be replaced by the `formContext` (from the function param), without any change to the rest of the line of code. E.g. `Xrm.Page.data.entity.getId();` becomes `formContext.data.entity.getId();` easily. At least for 95% of the code. Is that what you've found too? – Don Cheadle Oct 24 '18 at 21:09
  • I am reciving the primaryControl as null when trying to pass it as you suggested, with the difference that I am working on the home grid instead of a form. Online version, not UI. – Manuel Roldan Feb 12 '19 at 16:14
7

After passing the primaryControl as @scott-durow suggested, It is best to not use primaryControl.getFormContext() and instead use the primaryControl as though it is the formContext.

According to the documentation (1/2/2019): https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions#form-and-grid-context-in-ribbon-actions, one should perform operations on the primaryControl as though it is the formContext.

function mySampleFunction(primaryControl) {
    var formContext = primaryControl;
    // Perform operations using the formContext object
}

But, the key piece of the example provided is this: // Perform operations using the formContext object being the key (no idea why they added the var formContext = primaryControl line, imo, it would have been clearer if they had instead just shown an example: primaryControl.getAttribute('xxxx');

I suspect primaryControl.getFormContext() code started getting used because thats how you get the formContext when working with forms (https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/clientapi-form-context#using-the-formcontext-object-instead-of-the-xrmpage-object).

The problem with using primaryControl.getFormContext() is that it works with the normal Web interface, but breaks with the UCI. But if you use primaryControl as though it is the form-context, then it works for both the legacy web-client and uci interfaces.

Here is a function that I use:

function getFormContext(executionContext) {
     var formContext = null;
     if (executionContext !== null) {
         if (typeof executionContext.getAttribute === 'function') {
             formContext = executionContext; //most likely called from the ribbon.
         } else if (typeof executionContext.getFormContext === 'function' 
                 && typeof(executionContext.getFormContext()).getAttribute === 'function') {
            formContext = executionContext.getFormContext(); // most likely called from the form via a handler
         } else {
            throw 'formContext was not found'; //you could do formContext = Xrm.Page; if you like.
        }
    }
    return formContext;
}
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
  • Link to documentation as of when this is true (as v9 js seems to be changing often and this may not be true forever) https://github.com/MicrosoftDocs/dynamics-365-customer-engagement/blob/d1db343aeae119469f401e8d90f1d337bf4b3b21/ce/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions.md – Raj Rao Jan 02 '19 at 20:24
  • _It is best to not use primaryControl.getFormContext() and instead use the primaryControl as though it is the formContext._ This sentence saved me! Thank you for this!! This is very important! – SmallerMe Jan 05 '21 at 16:20
0

I was having the same issue as well. What I found out was, there was an error in Microsoft doco. Please follow whatever Scott mentioned passing CRM Parameter from ribbon command action. In javascript function, please try below to get form context

var formContext = primaryControl.getFormContext();

this fixed my issue.

Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
  • Indeed MS docs are wrong in this link https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions#form-and-grid-context-in-ribbon-actions in the area with `function mySampleFunction(primaryControl)` :( – Don Cheadle Oct 24 '18 at 20:21
  • Specifically how would you migrate from `Xrm.Page.context.getQueryStringParameters().etc;` ? – Don Cheadle Oct 24 '18 at 20:31
  • (answering own question above) -> that goes to this: `var formContext = primaryControl.getFormContext();` followed by `formContext.context.getQueryStringParameters().etc`..... so far it looks like *anywhere* you have Xrm.Page, just put the formContext in its place (after receiving it in the function param) – Don Cheadle Oct 24 '18 at 21:06
  • What I found is that "primaryControl.getFormContext();" works only in the web interface and does not work with UCI. Use primaryControl as though it is the form context. This will make your code work in both the web and UCI. (https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions#form-and-grid-context-in-ribbon-actions) – Raj Rao Jan 02 '19 at 19:36
  • @mmcrae, your code, breaks with the UCI. it is better to just do: primaryControl.context.getQueryStringParameters() (true as of v9.1.0.1006), which works for both the web and UCI – Raj Rao Jan 02 '19 at 19:43
  • @RajRao - you're right it's different when in a form vs a ribbon. I think you need different code in that case between ribbon-handling JS and form event JS. https://learn.microsoft.com/en-us/dynamics365/get-started/whats-new/customer-engagement/important-changes-coming#some-client-apis-are-deprecated ctrl+f for `Commands: Send it as the PrimaryControl parameter` – Don Cheadle Jan 15 '19 at 21:23
-1

There is a little trick you can do to not have to pass the Primary Control as Crm Parameter with the RibbonWorkbench utility, or if having done this, it would not be working for you, as it could happend if you were trying this in a home-grid ribbon.

var context=Xrm.Utility.getGlobalContext();

I hope this works for you or anyone else.

Manuel Roldan
  • 487
  • 3
  • 12