0
votes

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?

2
What pages??? You want change javascript default in pages?Mojtaba Nava
No, I dont want to change default of MS Dynamics CRM. I just want to add new functionality to my form as I save the form, some of fields contents concatenate in name field of form.babak jafari

2 Answers

3
votes

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

function onLoad(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

0
votes

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.