Setting values in a Dynamics 365 CE quick create form from the main form

Earlier this week I was asked to populate a field in a Dynamics 365 Customer Engagement quick create form with a value from a field on the main form. Unfortunately, the main form would not be saved at the time the quick create form was opened, so the value couldn't be read from the database.

While there is no good way to access the opening form from the quick create form using the Xrm.Page object model, there is a way to pass the value from the main form using the regular JavaScript browser object model. Because the quick create form and the main form are both children of the same topmost browser window, the main form can create a property in the top window that the quick create form can access when it loads.

Here's sample code that runs in the main form to set the topmost window's property value:

var setValsForQuickCreate = function(){
  window.top.attributename = Xrm.Page.getAttribute("new_attributename").getValue();
}

And here's the corresponding sample code to run when the quick create form loads:

var setValFromMainForm = function(){
  Xrm.Page.getAttribute("new_attributename").setValue(window.top.attributename);
}

This is a relatively simple example that assumes the value to set in the quick create form is the exact same value from the opening form, but there's no reason you can't do transformations if necessary. Additionally, there's no error checking here, so you'll probably want to at least add null checking/handling in the quick create form's script.