1

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

PickyTech
  • 135
  • 3
  • 9

3 Answers3

1

In function Xrm.Navigation.openForm argument formParameters is an object holding field values. The function passes these values to the fields that are (should be) on the form opened by it.

It is possible to pass custom query string parameters to the form. These parameters can be retrieved accessing the window.location object.

See openForm - MS Learn and Configure a form to accept custom querystring parameters - MS Learn.

The (classic) Form Properties dialog has a Parameters tab where custom parameters can be declared. Custom parameters that have not been configured here can only be passed in a plain url by adding an extraqs parameter.

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
1

You can access the form parameters from within the form by using the getFormContext() method. Here's an updated version of your code that shows how to access the formParameters object from within the form:

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) {
            var formContext = success.getFormContext();
            var viewName = formContext.getAttribute("viewName").getValue();
            console.log("View Name: " + viewName);
        },
        function (error) {
            console.log(error);
        });
}

In the success callback of the openForm method, you use getFormContext() to get a reference to the form context, and then use that reference to access the viewName attribute. You can then use the getValue() method on the attribute to retrieve its value.

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
steve 4239
  • 74
  • 2
  • That sounds good, but what happens on my onLoad event? – PickyTech Feb 10 '23 at 22:17
  • When the form opens, the onload fires. How do I access the parameters above? – PickyTech Feb 13 '23 at 19:10
  • @henk van Boeijen Please see my edited post. I got the error shown. – PickyTech Feb 14 '23 at 18:20
  • As far as I know the answer given here is incorrect. The success callback only returns an array with one item (`savedEntityReference`) containing some details regarding the navigation operation, but there is no form context. – Henk van Boeijen Feb 14 '23 at 20:57
  • That is correct it is incorrect. :) According to the MS documentation the savedEntityReference is only available after the form is saved. I need to have the value before save so I can hide a field value based on the selectedControl. – PickyTech Feb 14 '23 at 23:57
0

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);
    }
}


   
PickyTech
  • 135
  • 3
  • 9
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '23 at 02:07