0

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.

Slappy White
  • 79
  • 1
  • 12

1 Answers1

1

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.

Polshgiant
  • 3,595
  • 1
  • 22
  • 25
  • Thank you! That worked. May I ask how you do your version of cloning? I foresee there maybe being a problem with hitting the URL limit the way I am doing it. Does your solution account for that problem? – Slappy White Nov 21 '16 at 15:24
  • You're welcome. Nah, mine didn't need to be that robust. Just omitted certain fields that don't make sense to send to a create form like createdon, created by, modifiedon and so on. And also that type omission I mentioned in the answer. – Polshgiant Nov 21 '16 at 21:29
  • Hi, In my case it's didn't work. My field schema name is test_customerservices which is lookup field(User/Team). I have tried to pass three parameters which is test_customerservicesid, test_customerservicesname, test_customerservicestype but it's didn't work. – Kalp Shah Aug 16 '22 at 07:59
  • @KalpShah If the schema name is `test_customerservices`, then your first parameter should be `test_customerservices`, not `test_customerservicesid`. – Polshgiant Aug 19 '22 at 20:05
  • @Polshgiant Thanks for the help! It's working properly. Actually I have not added that fields in the forms so that it's not working. Once I added in the form it's working properly. – Kalp Shah Aug 29 '22 at 06:53