Hi
call bellow function on your Invoice attribute change, replace new_invoiceid and new_customername with your attribute names.
GetCustomerNameFromInvoice: function () {
/* Replace new_invoiceid with your Invoice relationship attribute name*/
var invoice = Xrm.Page.getAttribute("new_invoiceid").getValue();
if (invoice != null) {
var invoiceId = invoice[0].id.replace('{', '').replace('}', '');
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/invoices(" + invoiceId + ")?$select=_customerid_value", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var customerName = result["_customerid_value@OData.Community.Display.V1.FormattedValue"];
/* Replace new_customername with your customer single line text field*/
Xrm.Page.getAttribute("new_customername").setValue(customerName);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}