Hi,
This is because when there is no values, the statement "result.myLookupField.name" will throw an error because result.myLookupField would be null. I have tried and changed your code as below. Please try this,
===========
function GetParentAccount() {
var verboseLogs = true;
var account = Xrm.Page.getAttribute("new_account").getValue();
if (account != null && account[0] != null) {
var accountId = account[0].id.slice(1, -1);
Xrm.WebApi.retrieveRecord("account", accountId, "?$expand=parentaccountid($select=accountid,name)").then(
function success(result) {
if (result != null) {
if (verboseLogs) alert("Web API Called Successfully");
var myLookup = result.parentaccountid;
if (myLookup != null) {
if (verboseLogs) alert("myLookup: " + myLookup);
var myLookupName = result.parentaccountid.name;
if (verboseLogs) alert("myLookupName:" + myLookupName);
var myLookupId = result.parentaccountid.accountid;
if (verboseLogs) alert("myLookupId: " + myLookupId);
if (verboseLogs) alert("Setting to myLookup");
var myLookupArray = new Array();
myLookupArray[0] = new Object();
myLookupArray[0].id = myLookupId;
myLookupArray[0].name = myLookupName;
myLookupArray[0].entityType = "account";
Xrm.Page.getAttribute("parentaccountid").setValue(myLookupArray);
}
else {
Xrm.Page.getAttribute("parentaccountid").setValue(null);
}
}
else {
if (verboseLogs) alert("Clearing customerid");
Xrm.Page.getAttribute("parentaccountid").setValue(null);
alert("Please choose a valid account");
}
},
function (error) {
if (verboseLogs) alert(error.message);
});
}
}
==================