Setting the value of a Lookup Field using Javascript

Up Dynamics Creek > Microsoft Dataverse > Javascript > Setting the value of a Lookup Field using Javascript

Setting the value of a Lookup Field using Javascript

Ah Javascript, we meet again. I sat down and learnt this many years ago, and I use it so infrequently I often find myself needing a primer again. More over, we get to talk about the few times you have to use a bit of code.

Say you are opening a new record, (row?) and you want fields to be populated with specific value, well so many columns (fields) have options. An option set can have a default value, other fields have their value set via Business rules. Look up fields, those can be a bit of a pain. And something simple can be a major blocker for the Citizen Developers out there. Oh sure, you can use a power automate to set it, but, those are asynchronous (they do not apply instantly so the user is not aware) You could use a real time workflow to set it on create but that for your use case that’s not ok, or the row need that column populated before it would even allow the create.

So what do you do?

I wish Microsoft would expand the business rules to allow for this. How or never, this is a use case for some JavaScript and it is something I see plenty of newbies getting caught on. So lets go with the most basic complex ask.

Setting a lookup field using JavaScript assuming you know nothing about it.

I will gloss over the adding of Javascript on a form for now we will just get to the code you need. A quick google and you will find it yourself.

But here it is

var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = "{GUIDhere}"; // GUID of the lookup id
lookup[0].name = "Name"; // Name of the Row 
lookup[0].entityType = "contact"; //Table type (Entity type)
Xrm.Page.getAttribute("FieldName").setValue(lookup); // Replace "FieldName" with the fieldname you want set

So I will give an example

Say when I create a new Contact. I want the Company to be automatically filled with with the company “Up Dynamics Creek”

So I need to do a bit of legwork, I need to get the GUID of the record I want to set to, the Name that is displayed and of course the entity type. I need those of the record I am going to put INTO the field.

So that information is as follows:

GUID: 82bb3cb3-f647-ec11-8c62-000d3ada982b

Name: Up Dynamics Creek

Entity Type: account

The field on the Contact I want to set is: parentcustomerid

So when done the Code will look something like this.

var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = "{82bb3cb3-f647-ec11-8c62-000d3ada982b}"; // GUID of the lookup id
lookup[0].name = "Up Dynamics Creek"; // Name of the Row
lookup[0].entityType = "account"; //Table type (Entity type)
Xrm.Page.getAttribute("parentcustomerid").setValue(lookup); // Replace "FieldName" with the fieldname you want set

I left in the comments so you can do a direct compare. Now this wouldn’t be all you need you will need to know how to add Javascript to a form and how to get it to work. I will cover that in an upcoming post. But if all you needed was the code and an example here you go! Just change the fields you need too!

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Cookie Plugin by Real Cookie Banner