Get lookup field’s data on child form using JavaScript without querying the record

Problem Statement

We come across one of the frequent requirement to populate fields on child record’s form from the lookup field’s data. Whenever user changes the lookup field’s value, the respective information on other fields on the form should be populated from lookup record. Below is an example:

We have an Employee entity and CTC entity. On CTC entity, we have lookup of Employee record. Employee entity contains below fields:

  1. Name
  2. Joining Date
  3. Gender

CTC entity contains below fields:

  1. Total CTC Amount
  2. Employee (Lookup)
  3. Employee’s joining date
  4. Employee Gender

Now the requirement is, on CTC record, when employee is selected, Employee’s joining date and gender should be populated from the employee record.

Old way

Till now, we used to achieve this by registering JavaScript method on change event of Employee field on CTC. Then through JavaScript call, we used to fetch employee record’s data and set the value after getting results.

New way (CRM 2016 onwards)

With latest CRM 2016 release, Microsoft has introduced new methods to access quick view form’s fields using XRM object. So, it helped in bypassing an additional call to the record and directly fetch data from the form itself. Below are the steps to achieve this requirement without making call to the record.

    1. Add quick view form on employee entity and add Joining date and gender field on the form.
    2. On CTC entity form, add quick view form of employee entity.
    3. Add JavaScript event on employee field’s change event and register below method on the same:
function populateFieldsFromEmployeeRecord()
{
	if(Xrm.page.getAttribute("new_employee").getValue())//new_employee is lookup field on CTC record
	{
		var quickViewControl = Xrm.Page.ui.quickForms.get("");
		if (quickViewControl && quickViewControl.isLoaded())
		{
			var gender = quickViewControl.getControl(0).getAttribute().getValue(); //you can use quickViewControl.getControl(0).getName() method to validate the field's logical name
			var joiningdate = quickViewControl.getControl(1).getAttribute().getValue();
			Xrm.Page.getAttribute("new_gender").setValue(gender);
			Xrm.Page.getAttribute("new_joiningdate").setValue(joiningdate);
		}
	}
}

That’s it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s