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:
- In Function 001 - LaunchScenarioTrim below, can I use FirstPrimaryItemId, PrimaryEntityTypeName, and PrimaryEntityTypeCode as parameters to get the current open SCENARIO Record at the point of clicking the Add New Trim button on the sub grid?
- If not, how do I get the currently open Scenario Record and put it into Extraq Parameters from Entity 1 to pass to Entity 2's form that I open via the Add New Trim sub Grid button?
- Am I doing the correct thing with regard to passing the parameters below in Function 001 - LaunchScenarioTrim?
- How do I receive the passed parameters in Function 002 - OnLoad from Function 001 - LaunchScenarioTrim once the new Entity 2's form is open and executes the OnLoad function?
- How to I receive the SCENARIO record in Function 002 - OnLoad that I am hopefully be able to pass via the Function 001 - LaunchScenarioTrim?
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;