Announcements
I need to pass using JavaScript, a Record from Entity 1 via Form using Extraq Parameters to a Form on a Different Entity that I open by means of the Add New Record Sub Grid Button using Custom JavaScript.
I have two entities:
Entity 1 is named Scenario
Entity 2 is named Trim
There is an N:1 relationship between Trim and Scenario, meaning many Trim records can be associated with one Scenario record.
Scenario is part of a larger apparatus that is not relevant for this issue, however the larger unseen reality is dictating the method in use here.
When users go to a specific Scenario record, for this example we will use scenario named Sherwin Williams Emerald and open it up, they will see a tab called Trim and on the Trim Tab is a Sub Grid whose name is Detail1Detail2Detail3_SG.
I need to use Extraq Parameters on the Trim Entity’s Sub Grid whose name is Detail1Detail2Detail3_SG in the Scenario Entity Form, in such a way that when a User Clicks the Add New Trim Sub Grid Button and the Sub Grid’s name is Detail1Detail2Detail3_SG, JavaScript will execute on the Scenario Entity form, (a) passing the Sub Grid Name ofDetail1Detail2Detail3_SG and (b) getting and then passing the current Scenario Entity that’s open in the form at the time the Add New Trim Sub Grid Button is pressed. The goal being that when the new form opens on the related Trim entity, and the Onload event is triggered, the form/JavaScript receives the record that was present in the previous form so that record can be set as a Lookup Value in the Trim form.
I am able to perform (a) passing the Sub Grid Name of Detail1Detail2Detail3_SG as an Extraq parameter without incident. I use Ribbon Workbench and the SelectedControl.name parameter from Ribbon Workbench to achieve this.
My problem is with (b) getting and then passing the current Scenario Entity that’s open in the form at the time the Add New Trim Sub Grid Button is pressed. The goal being that when the new form opens on the related Trim entity, and the Onload event is triggered, the form/JavaScript receives the record that was present in the previous form so that record can be set as a Lookup Value in the Trim form.
I am not sure how to Get and pass the necessary Scenario Entity data from Sherwin Williams Emerald when the Add New Trim Sub Grid button is pressed.
FIGURE 001
FIGURE 002
On the Add New Trim Button in the Sub Grid, I have the following Ribbon Workbench Parameter Configuration.
FIGURE 003
The LaunchScenarioTrim function followed by the Onload functions are shown below.
The big questions are as follows:
FUNCTION 001 - LaunchScenarioTrim
function LaunchScenarioTrim(primaryControl, SelectedControl, FirstPrimaryItemId, PrimaryEntityTypeName, PrimaryEntityTypeCode) { "use strict"; var SubGridName = SelectedControl.name; var Detail1Detail2Detail3_SG = "Detail1Detail2Detail3_SG"; var MyOptionSetValue = "MyOptionSetValue"; var formContext = primaryControl; var globalContext = Xrm.Utility.getGlobalContext(); var client = Xrm.Utility.getGlobalContext().client.getClient(); if (client == "Web") { if (SubGridName == Detail1Detail2Detail3_SG) { var parameters = {}; parameters["MyOptionSetValue"] = 923190000; parameters["parameter_ScenarioId"] = FirstPrimaryItemId; //Is this the Guid of the Scenario Record that's open IN FIGURE001? parameters["parameter_ScenarioName"] = PrimaryEntityTypeName; //Is this the NAME of the Scenario Record that's open IN FIGURE001? parameters["parameter_ScenarioType"] = PrimaryEntityTypeCode; //Is this the TYPE of the Scenario Record that's open IN FIGURE001? var entityFormOptions = {}; entityFormOptions["entityName"] = "cpp_scenariotrim"; Xrm.Navigation.openForm(entityFormOptions, parameters).then( function (success) { console.log(success); }, function (error) { console.log(error); }); } } }
FUNCTION 002 - OnLoad
function OnLoad(executionContext) { var formContext = executionContext.getFormContext(); var form = Hsl.form(executionContext); var formType = formContext.ui.getFormType(); if (formType == 1) //New { var param = formContext.context.getQueryStringParameters(); var ScenarioId = param["parameter_ScenarioId"]; var ScenarioName = param["parameter_ScenarioName"]; var ScenarioType = param["parameter_ScenarioType"]; if (ScenarioId != undefined) { formContext.getAttribute("header_cpp_scenarioid").setValue([{ id: ScenarioId, name: ScenarioName, entityType: ScenarioType }]); } } }
We can see below, that FUNCTION 001 - LaunchScenarioTrim does retrieve the values as shown below.
The Guid, Entity Type Name, and Type Code are also retrieved in FUNCTION 001 - LaunchScenarioTrim.
The problem is when trying to pass them into the new form, on the other entity, they all come in as undefined.
.
How can we fix this so the 3 parameters can be used to set the Lookup field?
To properly set the lookup, we need the parameter values to equal data similar to what is shown below.
parameters["header_cpp_scenarioid"] = "abb20c00-f892-4668-a6f8-fed2078dd844";
parameters["header_cpp_scenarioname"] = "Sherwin Williams - Emerald";
parameters["header_cpp_scenariotype"] = "cpp_scenariotrim";
It seems I am unable to get the data shown above because the PrimaryEntityTypeName and PrimaryEntityTypeCode are different than the name of the record that corresponds to Guid abb20c00-f892-4668-a6f8-fed2078dd844. The data appears as shown below.
parameters["header_cpp_scenarioname"] = "cpp_scenariotrim"; parameters["header_cpp_scenariotype"] = 10566;
I fixed it. Here is the updated/revised code in the OnLoad function for anyone following who wants to see the difference.
This is a NAM D365 V9.2 Cloud Org.
function OnLoad(executionContext) { var formContext = executionContext.getFormContext(); var formType = formContext.ui.getFormType(); if (formType == 1) { var InteriorExterior = formContext.getAttribute("parameter_InteriorExterior").getValue(); var ScenarioId = formContext.getAttribute("parameter_ScenarioId").getValue(); var ScenarioName = formContext.getAttribute("parameter_ScenarioName").getValue(); var ScenarioType = formContext.getAttribute("parameter_ScenarioType").getValue(); if (ScenarioId != undefined) { var lookupArray = new Array(); lookupArray[0] = new Object(); lookupArray[0].id = ScenarioId; lookupArray[0].name = ScenarioName; lookupArray[0].entityType = ScenarioType; formContext.getAttribute("cpp_scenarioid").setValue(lookupArray); } if (InteriorExterior != undefined) { formContext.getAttribute("cpp_interiorexterior").setValue(InteriorExterior); } } }
It seems that I am halfway there based on what you have provided.
The LaunchScenarioTrim function works as expected.
The problem is with the OnLoad function receiving the parameters.
As you can see below, the getQueryStringParameters() does not appear to get the parameter_ScenarioId, parameter_ScenarioName, or parameter_ScenarioType. All 3 are undefined.
I have the parameters defined in the form as shown below.
What am I missing or doing wrong?
Hi ACECORP,
"It seems I am unable to get the data shown above because the PrimaryEntityTypeName and PrimaryEntityTypeCode are different than the name of the record that corresponds to Guid abb20c00-f892-4668-a6f8-fed2078dd844."
Yes, that's why you couldn't set the lookup field on your Trim form.
Actually, there is no need to pass those CRM parameters(FirstPrimaryItemId/PrimaryEntityTypeName/PrimaryEntityTypeCode) in the command. Instead, you could use getEntityReference(docs.microsoft.com/.../getentityreference) method in your LaunchScenarioTrim function.
So change your LaunchScenarioTrim function like this:
function LaunchScenarioTrim(primaryControl, SelectedControl, FirstPrimaryItemId, PrimaryEntityTypeName, PrimaryEntityTypeCode) {
"use strict";
var SubGridName = SelectedControl.name;
var Detail1Detail2Detail3_SG = "Detail1Detail2Detail3_SG";
var MyOptionSetValue = "MyOptionSetValue";
var formContext = primaryControl;
var globalContext = Xrm.Utility.getGlobalContext();
var client = Xrm.Utility.getGlobalContext().client.getClient();
var lookup = formContext.data.entity.getEntityReference();
if (client == "Web") {
if (SubGridName == Detail1Detail2Detail3_SG) {
var parameters = {};
parameters["MyOptionSetValue"] = 923190000;
parameters["parameter_ScenarioId"] = lookup.id; //Is this the Guid of the Scenario Record that's open IN FIGURE001?
parameters["parameter_ScenarioName"] = lookup.name; //Is this the NAME of the Scenario Record that's open IN FIGURE001?
parameters["parameter_ScenarioType"] = lookup.entityType; //Is this the TYPE of the Scenario Record that's open IN FIGURE001?
var entityFormOptions = {};
entityFormOptions["entityName"] = "cpp_scenariotrim";
Xrm.Navigation.openForm(entityFormOptions, parameters).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
});
}
}
}
And Onload function like this:
function OnLoad(executionContext) {
var formContext = executionContext.getFormContext();
var formType = formContext.ui.getFormType();
if (formType == 1) //New
{
var param = formContext.context.getQueryStringParameters();
var ScenarioId = param["parameter_ScenarioId"];
var ScenarioName = param["parameter_ScenarioName"];
var ScenarioType = param["parameter_ScenarioType"];
if (ScenarioId != undefined) { formContext.getAttribute("header_cpp_scenarioid").setValue([{ id: ScenarioId, name: ScenarioName, entityType: ScenarioType }]); }
}
}
And it can work on my side:
Click New Scenario Trim on the subgrid:
The lookup filed will be set:
BTW, It's really nice to provide so many details. It helps reproduce the error and solve it:)
André Arnaud de Cal...
294,120
Super User 2025 Season 1
Martin Dráb
232,871
Most Valuable Professional
nmaenpaa
101,158
Moderator