Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Count the Flag Array and display it on field

$
0
0

I want to count the duplicate values from flag array and display it on another field, but it gives error. So please help.

function Testing() {
var GUIDvalue = Xrm.Page.data.entity.getId();
alert(GUIDvalue);

var fetchXml = "<fetch mapping='logical' distinct='false'>"
+ "<entity name='account'>"
+ "<attribute name='new_choosedflag' 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.trim()+", ";
alert(flags);
}
}
Xrm.Page.getAttribute("new_flagvalues").setValue(flags);
var count = {};
flags.forEach(function(i) { count[i] = (count[i]||0) + 1;});
alert(count);
var uniqueList=flags.split(',').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(', ');
var final = uniqueList.slice(0, -3);
alert(uniqueList);
alert(final);
Xrm.Page.getAttribute("new_commonflag").setValue(final);
}
else {
console.log(req.statusText);
}
}
}


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images