javascriptcrmmicrosoft-dynamicsdynamics-crm-onlinexrmservicetoolkit

Javascript - Dynamics CRM Online - Passing a lookup field value to a form through openEntityForm


I am using Xrm.Utility.openEntityForm to clone a record. I have a group of attributes I need to copy over to the new form when I call openEntityForm. You do this by passing in a parameters object that is filled with the values of the attributes on the original form.

My question is: How do I pass the value of a lookup field as a parameter in the parameters object? I have a lookup field named "Department" -- I get the original value like this:

var department = parent.Xrm.Page.getAttribute("new_departmentid").getValue();

To set the value of the field on the cloned record, I initially create a parameter object -- var parameters = {}; -- and I set the value for the lookup field like this --

parameters["new_departmentid"] = department[0].id;

The parameters object gets passed to the openEntityForm method. This works to set the value of the new form's Department field, BUT the field reads "(No Name)".

I tried to do something like this:

parameters["new_departmentid"] = { id: department[0].id, name: department[0].name, entityType: "new_department" };

But that did not work, and I got errors saying parameter["new_departmentid"] expects a data type of "UniqueId".

How do I pull along the name to correctly populate the lookup field using this method? Thanks for any help.


Solution

  • I have a clone function that doesn't use openEntityForm, it just constructs a URL and then calls window.open. But it should be the same. You can pass lookup values by creating three separate parameters:

    1. one for the id using the field's id
    2. one for the name using the field's id + "name"
    3. one for the type using the field's id + "type".

    This should work for you:

    parameters["new_departmentid"] = department[0].id;
    parameters["new_departmentidname"] = department[0].name;
    parameters["new_departmentidtype"] = department[0].entityType;
    

    Bonus: Note that for lookups that can only take one entity type, you can omit the type parameter. It's only needed when passing values to lookups like Owner or Customer which can take multiple types. Omitting the parameter will allow you to pass more valuable information before hitting the URL limit of ~2k characters.