RE: When I create a new record from the new record form, it loses the relationship with the main entity
Use the below code.
You need to register this method on load. You also need to replace the field schema values.
=============
function loadParentDetailsToChild() {
try {
var parentAccountId = parent.top.opener.Xrm.Page.data.entity.getId();
var parentAccountName = parent.top.opener.Xrm.Page.getAttribute("name").getValue();
var parentAccountLookup = new Array();
parentAccountLookup[0] = new Object();
parentAccountLookup[0].id = parentAccountId;
parentAccountLookup[0].name = parentAccountName;
parentAccountLookup[0].entityType = "account";
Xrm.Page.getAttribute("parentcustomerid").setValue(parentAccountLookup);
}
catch (err) {
console.log(err.message);
}
}
==========================
Hope this helps.