Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to pass and read variable passed from one form to other form

Posted on by

Hello All Experts,

I need to open a quick create form of contact entity from one account form using custom button click.

but i need to also pass some value like account details like (id, name) which should get setup on the field present on the contact form.

Till now .

1. I have customized the button using ribbon workbench and called a function which can open the quick create form.

2. I am not sure how to set the value in the account lookup field present on the contact form.

I am utilizing below code to open quick create form.

function openquickCreateForm(){

// *** First get the Id of the required account, possibly like this ....
var accountValue = null;
var account = Xrm.Page.getAttribute("new_account");
if (account != null) {
accountValue = account.getValue();
if (accountValue != null) {
var accountId = accountValue[0].id;
}
}

// ** Create an object including the accountId. Used to call the
// ** quick create form in the context of the account
var parentAccount = {
entityType: "account",
id: accountId,
name: accountValue.name
};

// *** NOTE: Here we aren't setting any parameters! (But we could!)
var parameters = { };

// *** Call the Xrm.Utility needed to add a contact.
Xrm.Utility.openQuickCreate("contact", parentAccount, parameters).then(function (lookup) { successCallback(lookup); }, function (error) { errorCallback(error); });

// *** Function called on success.
function successCallback(lookup) {
alert("lookup: " + lookup.savedEntityReference.id);
alert("lookup: " + lookup.savedEntityReference.name);
}

// **** Function called on error.
function errorCallback(e) {
// *** No new contact created, which is an error we can ignore!
alert("Error: " + e.errorCode + " " + e.message);
}

}

and trying to bind the data into the account lookup using below function

function bindAccountlookup()

{

if (Xrm.Page.ui.getFormType() == 1) {
var lookupValue = new Array();
lookupValue[0] = new Object();
var queryParam = Xrm.Page.context.getQueryStringParameters();
if (queryParam.Data) {
arrayObject = JSON.parse(queryParam.Data);
lookupValue[0].id = arrayObject[0].id;
lookupValue[0].name = arrayObject[0].name;
lookupValue[0].entityType = "account"; //Entity Type of the lookup entity
Xrm.Page.getAttribute("new_account").setValue(lookupValue);
}
}

}

The above two highlighted lines do not have that lookup value which i need to set.

Any help will be appreciable.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to pass and read variable passed from one form to other form

    Hi Partner,

    You can use following code to achieve it.

    function createContact() {
        var thisAccount = {
            entityType: "account",
            id: Xrm.Page.data.entity.getId()
        };
        var callback = function (obj) {
            console.log("Created new "   obj.savedEntityReference.entityType   " named '"   obj.savedEntityReference.name   "' with id:"  
                obj.savedEntityReference.id);
        }
        var setName = { firstname: "Contact Name for "   Xrm.Page.getAttribute("name").getValue() };
        Xrm.Utility.openQuickCreate("contact", thisAccount, setName).then(callback, function (error) {
            console.log(error.message);
        });
    }

    Test result:

    pastedimage1616399279192v1.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Verified answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: How to pass and read variable passed from one form to other form

    Hi,

    Thank you for your query.

    What I understand is you need to open a new Contact form and set Parent Account lookup on this form.

    I see following problems in your code:

    1 - Xrm.Utility.openQuickCreate("contact", parentAccount, parameters).then(function (lookup) { successCallback(lookup); }, function (error) { errorCallback(error); });

    You are passing 2 objects as parameters, however openQuickCreate method only accepts second one as parameters.

    2 - As far as I know, form parameters are Query String params. not Json Objects.

    See following example:

    var entityFormOptions = {};
    entityFormOptions["entityName"] = "contact";
    entityFormOptions["useQuickCreateForm"] = true;
    
    // Set default values for the Contact form
    var formParameters = {};
    
    // Set lookup field
    formParameters["parentaccountid"] = accountId; 
    formParameters["parentaccountidname"] = accountValue.name;
    formParameters["parentaccountidtype"] = "account"; 
    // End of set lookup field
    
    // Open the form.
    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
        function (success) {
            console.log(success);
        },
        function (error) {
            console.log(error);
        });
      

    You can find details here:

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openform#example-3-open-a-quick-create-form

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