Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Concatenation and remove duplicate values from array

(0) ShareShare
ReportReport
Posted on by 940

Check the blue colored code i want to concatenate the array in new flags and return it to the field in CRM which is commonflag. But it alerts [Object Object]... and its repeated pairs. So, please correct this one.

Untitled97.png

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="";
for (var i = 0; i < results.value.length; i++)
{
alert("inside for");
flags += results.value[i]+", ";
alert(flags);
}
var newResult = flags.slice(0, -2);
Xrm.Page.getAttribute("new_commonflag").setValue(newResult);
}
else {
console.log(req.statusText);
}
}
}/*
var totalcount = results.value[0].recordcount;
Xrm.Page.getAttribute("new_orgcasecount").setValue(totalcount);*/

*This post is locked for comments

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,205 on at
    RE: Concatenation and remove duplicate values from array

    Hi,

    First you need to create an array and add all your flag in that, that using that array you need to pull out the unique values, after that you need to append all the flag seprated by comma.

    Below is the code, please use it and try,

    var arr = new Array(); //new array

    var flags="";

    alert(results.value[0]);

    for (var i = 0; i < results.value.length; i++)

    {

    if(results.value[i].flagstring!= undefined)

    {

    arr.push(results.value[i].flagstring); // push all the flag in array

    }

    }

    var uniquearray = []

      for(let i = 0;i < arr.length; i++){

          if(uniquearray.indexOf(arr[i]) == -1){

              uniquearray.push(arr[i]);

    flags += arr[i]+ ","; // append all the flags in string comma seprated

          }

      }

    flags = flags.replace(/,\s*$/, ""); //remove the last comma from flag string

    if you find it helpful, please mark as verified.

    Best Regards,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,205 on at
    RE: Concatenation and remove duplicate values from array

    Hi Sakti,

    Please check this link,

    community.dynamics.com/.../280809

    Best Regards,

    Shahbaaz

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Concatenation and remove duplicate values from array

    Hi ,

    Declare the variable at begining of your function.

    var flagval= null;

    Replace the result part-

    var results = JSON.parse(this.response);

                 for (var i = 0; i < results.value.length; i++) {                        

                  var accountDetails = results.value[i];

    flagval+=accountDetails['new_commonflag']+",";

    }

    Xrm.Page.getAttribute("new_commonflag").setValue(flagval);

  • RE: Concatenation and remove duplicate values from array

    Thanks das, but concatenate is not easy in above code can u make correction in that code.

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Concatenation and remove duplicate values from array

    Hi ,

    If you are not adding the fields in the form you can not set the value to other form fields of same entity.  Without adding fields control will not found when you try to set value.

    For best practice add the other form fields of same entity in the same form and make it hidden and then set the value. When you move other form dynamics crm will save the first form and same field you can display in other form which will display your concatenated value in the first form.

    If your entity is different then make sure you need to make update request to change the value of other entity fields.

  • RE: Concatenation and remove duplicate values from array

    I want to concatenate the data from an field of account entity to different entity, but both fields are on different form of account entity. So, is this effect the code.

  • Suggested answer
    Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Concatenation and remove duplicate values from array

    Hi Sakthi,

    Can you make following changes.

    for (var i = 0; i < results.value.length; i++)
    {
    alert("inside for");
    var result = results.value[i];
    flags += result['new_choosedflag']+", ";
    alert(flags);
    }

    Thanks

    Justin Jose

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Concatenation and remove duplicate values from array

    Hi Shakti ,

    You need to retrieve the value from the result , it will not give you directly the value.

    Please follow below article I hope this will helps you.

    community.dynamics.com/.../use-fetchxml-to-retrieve-data-from-ms-crm-2016-using-web-api

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans