Hi all,
The below works to filter out duplicates also. Please find below for anyone who may find it useful.
function Testing() {
var GUIDvalue = Xrm.Page.data.entity.getId();
alert(GUIDvalue);
var fetchXml = "<fetch mapping='logical' distinct='false'>"
+ "<entity name='account'>"
+ "<attribute name='new_teamflags' alias='flagstring'/>"
+ "<link-entity name='account' from='accountid' to='accountid' link-type='inner' alias='ab'>"
+ "<filter type='and'>"
+ "<condition attribute='accountid' operator='under' value='" + GUIDvalue + "' />"
+ "</filter>"
+ "</link-entity>"
+ "</entity>"
+ "</fetch>";
alert(fetchXml);
var fetch = encodeURI(fetchXml);
var entityname = "accounts";
var serverURL = Xrm.Page.context.getClientUrl();
var Query = entityname + "?fetchXml=" + fetch;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false); //Removed api from url domain
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.send();
if (req.readyState == 4) { /* complete */
req.onreadystatechange = null;
if (200 == req.status) {
debugger; // Try to debug in this point to get the count if any error you get
var results = JSON.parse(req.response);
var flags="";
alert(results.value[0]);
for (var i = 0; i < results.value.length; i++)
{
if(results.value[i].flagstring!= undefined)
{
alert("inside for");
flags +","+= results.value[i].flagstring;
alert(flags);
}
}
var final = flags.split(",").filter(function(allItems,i,a){
return i=a.indexOf(allItems);
}).join(',');
alert(final);
Xrm.Page.getAttribute("new_testfield").setValue(final);
}
else {
console.log(req.statusText);
}
}
}