Search code examples
dynamics-crmmicrosoft-dynamics

executionContext vs primaryControl


I am having issues using the same code in two scenarios:

  1. From a ribbon button call revisionRequested which in turn calls removeOptions and
  2. from the form OnLoad, call removeOptions only.

The code below is my work around, but I what I originally tried was to pass the primaryControl param from revisionRequested to removeOptions in which I made formContext = primaryControl I got an error.

    function revisionRequested(primaryControl) {
    
        var formContext = primaryControl;
        formContext.getAttribute("statuscode").setValue(100000009);  // Revision Requested
        removeOptions(0, primaryControl);
    
        Xrm.Navigation.openAlertDialog('Your revision request has been been submitted');
    }
    
    function removeOptions(executionContext, primaryControl) {
        var formContext = null;
        executionContext == 0 ? formContext = primaryControl : formContext = executionContext.getFormContext();
    
        var statusCode = formContext.getAttribute("statuscode").getText();
        var statusControl = formContext.getControl("header_statuscode");
    
        if (Xrm.Page.ui.getFormType() !== 1 && statusCode.includes('Revision')) {
            statusControl.removeOption(1);
            statusControl.removeOption(100000000);
            statusControl.removeOption(100000001);
            statusControl.removeOption(100000002);
            statusControl.removeOption(100000003);
            statusControl.removeOption(100000003);
            statusControl.removeOption(100000004);
            statusControl.removeOption(100000005);
            statusControl.removeOption(100000006);
            statusControl.removeOption(100000007);
            statusControl.removeOption(100000008);
        }
    }

Solution

  • I believe what you want is polymorphism in JavaScript for removeOptions method, one with executionContext signature to use from form load, another one with primaryControl signature to use from ribbon. These two signatures are not same of course, and passing execution context from the form while registering event handlers have only choice to send it as first parameter.

    What you did is valid workaround to utilize the reusable method in two different places with all limitations and platform design into consideration.

    Read this discussion for more understanding.