Tuesday, 15 November 2022

Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench

 

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

  1. Let’s say you have a ribbon on the Opportunity that does something based on budget, so I’ve called it Budget.

  2. 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

  3. Once you select CRM Parameter, you’ll need to pass on the Primary Control as shown below

  4. 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,

  1. We were Passing CRM Parameter PrimaryControl to Button in Ribbon workbench

    image

  2. 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(); 
             }
        }
    }
  3. But when same is called in UCI we get ” ” Script Error, Because here we need to treat primary control as formContext

    image

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

Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...