D365 V9{Upgrade}: Ribbon Button Enable Rule Custom JS Code gives error “Cannot read property ‘getFormContext’ of undefined”

[Update]: The PrimaryControl CRM parameter passed is formContext which can be used further to get the value of the attribute.

While working on upgrade activities of D365 V9 from D365 V8, we faced one weird issue related to Enable Rule of  Ribbon button.

We had one form where we were using Enable Rule for a button which was defined using custom JS code. As part of client side scripting changes related to D365 V9 upgrade, we passed Primary Control as first CRM parameter in Ribbon Workbench for that Enable Rule as explained here and in JS we passed executionContext as a parameter as shown below:


DisplayReadyForPMU: function (executionContext) {
debugger;
var formContext = executionContext.getFormContext();
var campaignType = formContext.getAttribute("sab_outputtype").getValue();
...
}

While testing after making the above changes, we got the error “Cannot read property ‘getFormContext’ of undefined”

aaaa

While debugging the code using Developer Tools in browser, we found that executionContext is undefined

cccccc

Then, continuing execution, we found that the method/debugger was hit again and this time we got the value in executionContext

bbbbb

This was a strange behavior that custom JS code for Enable Rule of a button got executed twice. To fix this issue, we added null check for executionContext. After adding null check we didn’t get the above error anymore. However, it’s still a mystery that the JS code block/method was getting executed twice.

DisplayReadyForPMU: function (executionContext) {
debugger;
if (executionContext != null) {
var formContext = executionContext.getFormContext();
var campaignType = formContext.getAttribute("sab_outputtype").getValue();
...
}
...
}

Hope it helps !!

Advertisement

8 thoughts on “D365 V9{Upgrade}: Ribbon Button Enable Rule Custom JS Code gives error “Cannot read property ‘getFormContext’ of undefined”

    1. Hi Eduard,

      Thanks for your input. We are already passing primaryControl “CRM parameter” as the first parameter in the Ribbon Workbench. We have tried using primaryControl as formContext object as you have mentioned. Still we are getting this issue.

      Like

    1. The PrimaryControl CRM parameter passed is formContext which can be used further to get the value of the attribute as Eduard has mentioned in one of the comments above.

      Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.