How to set lookup fields using JS

| Hafsa Farooqui

Dynamics 365 and Model Driven apps offer a wide range of customization options, including the ability to use JavaScript to manipulate form data. To set a lookup value in Dynamics 365/Model Driven apps using JavaScript, you should follow these steps:

Step 1: Get the attribute object for the lookup field

The first step is to get the attribute object for the lookup field. You can do this using the getAttribute method of the Xrm.Page object. The getAttribute method takes the logical name of the attribute as an argument and returns an Xrm.Page.Attribute object.

In this example, new_lookupfield is the logical name of the lookup field you want to set the value for.

Step 2: Create an array to hold the lookup value

Next, create an array to hold the lookup value. The lookup value is an array of objects that represent the records being looked up. In our case, we're looking up a single record, so our array will only contain one object.

Step 3: Create a new object to represent the record being looked up

Next, create a new object to represent the record being looked up. The object should have two properties: id and entityType. The id property should contain the GUID of the record being looked up, and the entityType property should contain the logical name of the entity being looked up.

In this example, {D22F57F5-6B5D-EB11-A813-000D3A55CC4C} is the GUID of the record being looked up, and account is the logical name of the entity being looked up.

Step 4: Set the lookup value in the array

Next, set the lookup value in the array. The lookup value should be an array of objects representing the records being looked up. In our case, we only have one record, so our array will only contain one object.

Finally, set the lookup value in the attribute using the setValue method of the Xrm.Page.Attribute object.

And that's it! You've successfully set the value of a lookup field using JavaScript.


Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.


M
Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.


More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting