Search code examples
dynamics-crmpowerappsdataversepowerapps-modeldriven

How to Access Parameters in New Form


I have a custom button that I have the below JavaScript attached to that opens an entity form and I am trying to pass viewName to it.

function sendContextToQC(selectedControl) {
    var entityFormOptions = {};
    entityFormOptions["entityName"] = "new_qrecipemasteritem";
    entityFormOptions["useQuickCreateForm"] = true;

    var currentView = selectedControl.getViewSelector().getCurrentView().name;

    var formParameters = {};
    formParameters["viewName"] = currentView

    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
        function (success) {
            console.log(success);
        },
        function (error) {
            console.log(error);
        });
}

The form opens fine and I have the "Pass Execution Context as first parameter" checked, but I don't know how to access the formparameters object. Is it part of the executionContext? I even tried adding another parameter (formParameters), but that didn't work either.

2/14/23 Error


Solution

  • I figured it out. To send parameter:

    function sendContextToQC(selectedControl) {
    var entityFormOptions = {};
    entityFormOptions["entityName"] = "new_qrecipemasteritem";
    entityFormOptions["useQuickCreateForm"] = true;
    
    var currentView = selectedControl.getViewSelector().getCurrentView().name;
    
    var formParameters = {};
    if (currentView.includes("Master") == true) {
        formParameters["isMasterView"] = 1;
     }
     else {
        formParameters["isMasterView"] = 0;
    }
    
    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
        function (success) {
            console.log(success);
        },
        function (error) {
            console.log(error);
        });
    } 
    

    Then to pick it up onload. The key below is buttonData = Xrm.Utility.getPageContext().input. Data;

    this.hideUsedIn = function(executionContext) {
        let formContext = executionContext.getFormContext();
        let usedInAttribute = formContext.getAttribute("new_usedin");
        let usedInControl = formContext.getControl("new_usedin");
       
        let buttonData = {};
        buttonData = Xrm.Utility.getPageContext().input.data;
        let isMasterItem = buttonData.isMasterView;
        
        if (isMasterItem === 1){
        usedInAttribute.setRequiredLevel("none");
        usedInControl.setVisible(false);
        }
    }