Skip to main content

How To: Set Lookup value using Javascript in Dynamics CRM Portal

If you work on CRM portals and mostly into writing JavaScript code then this is for you!!
Two kinds of people land on this blog post
  • To have a quick solution - For you, I will provide the solution first :)
  • To learn something new - For you, ill explain the solution in detail later

Quick Solution
As you want to set the lookup value, keep below values of the lookup handy (in variables) in JavaScript code
     1. GUID: Guid value of the Target Entity
     2. Name: Name field of the Target Entity to display as the selected lookup value.
     3. EntityName: Enity Name of the Target Entity.

Use the above three values as shown below! Done your look up should be set after execution of below JavaScript code.


Understanding the Solution
CRM lookup features:
     1. Can hold any type of entity
     2. Always displays the value of 'Name' field (Which is required and mandatory for all the entities in CRM)

Now if we Inspect the Lookup Element using Developer Toolbar. Below is the html rendered for lookup, it renders as two sections
     1. Group of Input Controls - This has your search box, icon to click and textbox to enter data.
     2. Lookup Model - This holds the layout and data which is to be shown as a dropdown when you enter some text in the Lookup textbox.

As we are more interested in setting the lookup value for this blog post, lets see what is in the 'input-group' and what we can do to set the value of the lookup


We find three textboxes out of which two are hidden (GUID and Entity Name) and one is visible which displays Name field (Which is shown to end user)
Following these three textboxes, we have 'input-group-btn' which holds the search icon with button.

Setting the values of these three values as discussed earlier in 'Quick Solution' should suffice for the lookup to know which entity to show up and set the selected entity accordingly.

Want to learn more?? then explore the lookup model 'div' also which holds the layout details, model data, end user messages in the dropdown as we type-in the lookup control..

Comments

Post a Comment