Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to Pass JavaScript Entity 1 Record Data via Extraq Parameters to Entity 2 via JavaScript by means of Add New Record Subgrid

Posted on by 1,579

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

SCENARIO.png

FIGURE 002

SCENARIOTRIM.png

On the Add New Trim Button in the Sub Grid, I have the following Ribbon Workbench Parameter Configuration.

FIGURE 003

ScenarioTrimRW.png

The LaunchScenarioTrim function followed by the Onload functions are shown below.

The big questions are as follows:

  1. 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?
  2. 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?
  3. Am I doing the correct thing with regard to passing the parameters below in Function 001 - LaunchScenarioTrim?
  4. 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?
  5. 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.

Screen-Shot-2021_2D00_10_2D00_26-at-6.47.44-PM.png

The problem is when trying to pass them into the new form, on the other entity, they all come in as undefined.

Screen-Shot-2021_2D00_10_2D00_26-at-6.48.23-PM.png.

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;

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to Pass JavaScript Entity 1 Record Data via Extraq Parameters to Entity 2 via JavaScript by means of Add New Record Subgrid

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

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to Pass JavaScript Entity 1 Record Data via Extraq Parameters to Entity 2 via JavaScript by means of Add New Record Subgrid

    It seems that I am halfway there based on what you have provided. 

    The LaunchScenarioTrim function works as expected.

    LaunchFn.png

    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. 

    OnLoad.png

    I have the parameters defined in the form as shown below. 

    ParamInForm.png

    What am I missing or doing wrong?

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Pass JavaScript Entity 1 Record Data via Extraq Parameters to Entity 2 via JavaScript by means of Add New Record Subgrid

    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:

    pastedimage1635323830103v1.png

    The lookup filed will be set:

    pastedimage1635323938324v2.png

    BTW, It's really nice to provide so many details. It helps reproduce the error and solve it:)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans