Tag: how to pass value from child form to parent form dynamics ccr

Populate Data From Child Record To Parent Record’s Quick Create Form In Dynamics 365

Populating data on forms in CRM is not a headache since you can do mappings for your 1:N record or simply do a Web API request using the parent lookup exist on the new form to retrieve the values from that record and set them on the current form.

However what if you have to populate data on a parent record form from a child record form? there is no out of the box functionality or script available for this. I digged in further and found below method. Its a rare but an important requirement. 

  1. I have an entity named “Potential Customer” which has a lookup of Contact along with some other fields such as name, contact ref number etc. My requirement is to pass name and contact ref from potential customer to contact on quick create form:
  2. Here are the two field below the parent contact lookup which I would like to be propagated to contact quick create form:
  3. To achieve this, I have used a concept of localStorage property.
  4. Create below javascript resource and add to your child record form( from where you will add new parent record) in my case its potential customer:
function SetLocalStorage(executionContext)
{

var formContext = executionContext.getFormContext();
var firstName = formContext.getControl("new_name").getAttribute().getValue().toString();
var refNumber = formContext.getControl("new_contactrefnumber").getAttribute().getValue();

localStorage.setItem("FirstName",firstName );
localStorage.setItem("ContactRef",refNumber );

}

In the code above I am simply retrieving the value from current record on load event and setting it to the localStorage. You have to set value using a key and the value. so from code below; FirstName is the key(using which we will retrieve the value) and firstName variable is the value.

localStorage.setItem(“FirstName”,firstName );

       
         5. Now add below code to your onload quick create form of the parent record in my case its contact.         

function RetrieveStorageValue(executionContext)
{
debugger;
var formContext = executionContext.getFormContext();
var firstName = localStorage.getItem("FirstName");
var contactRef = localStorage.getItem("ContactRef");

formContext.getAttribute("firstname").setValue(firstName);
formContext.getAttribute("new_contactref").setValue(contactRef);

localStorage.removeItem("FirstName");
localStorage.removeItem("ContactRef");

}

In the above code i am simply retrieving the value set using localStorage.getItem(“KeyName”); and then setting it on the required fields.

Use this link explained very well by Neil on how to add javascript code to your form : JS Basics

So now when Click on the new button from the lookup dropdown to add a parent record , I get values populated:

 

Here you go! 

Few important points:  

  1. I don’t encourage using properties and methods which are not defined by/for Microsoft dynamics CRM.
  2. The data you add to localStorage doesn’t have expiration which means the data will be there unless you don’t clear it (hence in my code I clear it everything once I have  used the values)
  3. It is just one of the methods (sometimes referred to as “Unsupported”) to achieve this requirement.
  4. The above script is for Dynamics crm 365 v9 or above. Please use Xrm.Page instead of formContext if you wish to implement this in earlier versions.

 

I hope this helps!