Skip to content Skip to sidebar Skip to footer

How Can I Get The Value Of A Lookup Field By Javascript In Microsoft Dynamics Crm 365

I am trying to get a lookup field value and set the value into another field ('Name' field for example) by Javascript in Microsoft Dynamics CRM. How can I do it?

Solution 1:

To use the new (CRM 365) methods you need to do two things:

When you write your form library, your function must include a parameter. This is set by CRM when it calls your function. In my example here, the parameter name is executionContext but the name does't matter

Once you have this CRM parameter, you can get the Form Context which is the new Xrm.Page equivalent. See below

functiononLoad(executionContext)
{
  var formContext = executionContext.getFormContext();

  var lookup = formContext.getAttribute("new_account").getValue();
  formContext.getAttribute("new_name").setValue("Your Account Name is:" + lookup[0].name);
}

Secondly, when you register your form library, you must pass the Execution Context. This is what tells CRM that your form library method has the executionContext parameter that must be set

Pass execution context

Solution 2:

I found it on docs.microsoft. To do this, first you should know about Document Object Model in Dynamics CRM which is called "Xrm":

var lookupValue=Xrm.Page.data.entity.attributes.get('new_account').getValue()[0].name;
Xrm.Page.getAttribute("new_name").setValue("Your Account Name is:"+lookupValue);

You can use it as a function and call it on save (OnSave) event of Microsoft dynamics CRM Form.

Post a Comment for "How Can I Get The Value Of A Lookup Field By Javascript In Microsoft Dynamics Crm 365"