Try following:
function updateaccount()
{
var contactid = Xrm.Page.getAttribute("primarycontactid").getValue();
if (contactid == null)
return;
contactid = contactid[0].id.replace("}", "").replace("{", "");
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts(" + contactid + ")?$expand=parentcustomerid_account($select=accountid,name)", true);
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=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var contactid = result["contactid"];
if(result.hasOwnProperty("parentcustomerid_account")) {
var parentcustomerid_account_accountid = result["parentcustomerid_account"]["accountid"];
var parentcustomerid_account_name = result["parentcustomerid_account"]["name"];
Xrm.Page.getAttribute("customerid").setValue([{
id: parentcustomerid_account_accountid,
name: parentcustomerid_account_name,
entityType: "account"
}]);
}
}
else {
alert(this.statusText);
}
}
};
req.send();
}