Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench
One of the most common searches around customizing ribbon buttons is to send the Execution Context / Form Context as a parameter to the JavaScript function your Ribbon button is set to call.
Pass Form Context to the JS in Ribbon
Let’s see how you can pass form context as a parameter to the JS method on from your Ribbon button
- Let’s say you have a ribbon on the Opportunity that does something based on budget, so I’ve called it Budget.
- In the Ribbon Workbench, the button is calling the command which has a JS Function tied to it. Add a CRM Parameter to the JavaScript Action
- Once you select CRM Parameter, you’ll need to pass on the Primary Control as shown below
- Then, it’ll look like this on your JS Function
Accessing Form Context
Let’s say you have opened the Dev Tools on your browser by pressing F12, then click the button (assuming you either have set a breakpoint inside the function or using debugger; to stop at a point inside the function that button calls). And then you click the button
Javascript code for ribbon customization
We got this error in UCI and not in Legacy WebClient,
- We were Passing CRM Parameter PrimaryControl to Button in Ribbon workbench
- In JS we were getting FormContext from primaryControl(Executioncontext) which was working fine in the Legacy app
function readXML(primaryControl) { var formContext = null; if (primaryControl!== null) { formContext = primaryControl.getFormContext(); } } }
- But when same is called in UCI we get ” ” Script Error, Because here we need to treat primary control as formContext
So final change needed in Code is as below
function readXML(primaryControl) { var formContext = null; if (primaryControl!== null) { if (typeof primaryControl.getAttribute === 'function') { formContext = primaryControl; //called from the ribbon. } else if (typeof primaryControl.getFormContext === 'function' && typeof(primaryControl.getFormContext()).getAttribute === 'function') { formContext = primaryControl.getFormContext(); // most likely called from the form via a handler } } }
No comments:
Post a Comment