This fetch query below will retrieve an Opportunity record but when I try to get the ID and Name of the parentaccountid lookup field, which I know has a value, all that gets returned is 'undefined'. Is this a syntax problem or are we unable to get lookup fields from FetchXML?
function SetAccount() {
try {
var OppId;
var OppName;
var OppType;
//get the OpportunityId from the Quote that is loading
if (Xrm.Page.getAttribute("opportunityid").getValue() != null) {
OppId = Xrm.Page.getAttribute("opportunityid").getValue()[0].id;
OppName = Xrm.Page.getAttribute("opportunityid").getValue()[0].name;
OppType = Xrm.Page.getAttribute("opportunityid").getValue()[0].entityType;
}
console.log("**************** OppId=" + OppId + " OppName=" + OppName + " OppType=" + OppType);
var FetchXML = "<?xml version='1.0'?>" +
"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0' >" +
"<entity name='opportunity' >" +
"<attribute name='name' />" +
"<attribute name='parentaccountid' />" +
"<attribute name='parentcontactid' />" +
"<order attribute='parentaccountid' descending='true' />" +
"<filter type='and'>" +
"<condition attribute='opportunityid' value='" + OppId + "' uitype='opportunity' uiname='" + OppName + "' operator='eq'/>" +
"</filter>" +
"</entity >" +
"</fetch >"
 
var encodedFetchXML = encodeURI(FetchXML);
var queryPath = "/api/data/v8.0/opportunities?fetchXml=" + encodedFetchXML;
var requestPath = Xrm.Page.context.getClientUrl() + queryPath;
var req = new XMLHttpRequest();
req.open("GET", requestPath, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText);
var results = returned.value;
var accountName = "";
for (var i = 0; i < results.length; i++) {
accountName = results[i]["name"]; //This return the name
console.log("---name = " + accountName);
var tacct = results[i]["parentaccountid"]; //RETURNS UNDEFINED, when I know a value is there
console.log("---tacct = " + tacct);
if (tacct != null) {
var acctguid = tacct[0].id;
var acctname = tacct[0].name;
Xrm.Page.getAttribute("falcon_account").setValue([{ id: acctguid, name: acctname, entityType: "account" }]);
}
Xrm.Page.getAttribute("falcon_quotenote1").setValue("name = " + accountName);
}
}
};
req.send();
} catch (err) {
console.log("************* ERROR **************" + err.message);
}
console.log("%%%%%% DONE WITH SCRIPT %%%%%%%%%%");
}