Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Fetch text field value from multiple account records

Posted on by Microsoft Employee

Hi experts,

In the account entity, I have created 2 forms: 1st for Parent Account and 2nd for Child Accounts

I have a simple text field on the child account form called new_teamflags.

When viewing the parent account record, I would like to fetch the value of new_teamflags from all of its child accounts and concatenate and store as a string on the parent account form.

How can I achieve this using Javascript? (assuming this cannot be done out of the box)

Thanks,

Jon

*This post is locked for comments

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch text field value from multiple account records

    Hi Jon,

    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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Fetch text field value from multiple account records

    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);

                  }

          }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Fetch text field value from multiple account records

    Hi Shahbaaz,

    I figured out the error. Please find below my working code: 

    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);
    }
    alert(final);
    Xrm.Page.getAttribute("new_testfield").setValue(flags);
    }
    else {
    console.log(req.statusText);
    }
    }
    }

    My output looks like this:

    Screen-Shot-2018_2D00_05_2D00_16-at-11.49.02-AM.png

    Now, I want to filter out the duplicate values in the string and display only once. So in the screenshot above "Red Flag" should be displayed only once. 

    The code in pink is what how I'm trying to filter the string but I'm getting script error. Would you know how I can fix this?


    Thanks,

    Jon

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch text field value from multiple account records

    Hi Jon,

    Best way is to debug the code, try to check the value of "results.value[i]["new_testfield"]"  in console then only you will get the clear idea what you are getting in result.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Fetch text field value from multiple account records

    Hi Shahbaaz,

    Now it is showing as:

    Screen-Shot-2018_2D00_05_2D00_15-at-5.28.50-PM.png

    Thanks,

    Jon

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch text field value from multiple account records

    Please correct this line : flags += results.value[i];  

    to : flags += results.value[i]["new_testfield"];

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Fetch text field value from multiple account records

    Hi all,

    I used the following code, but now it is storing inaccurate values like this: 

    Screen-Shot-2018_2D00_05_2D00_15-at-4.44.47-PM.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_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="";
                       for (var i = 0; i < results.value.length; i++)
                       {
                          alert("inside for");
                          flags += results.value[i];
                       }
                          Xrm.Page.getAttribute("new_testfield").setValue(flags);
                   }
                   else {
                       console.log(req.statusText);
                   }
           }
    }


    Thanks,

    Jon

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Fetch text field value from multiple account records

    Hello Jon,

    I will suggest you to debug your code and check if request is forming correct, and you can look for the result and check returned property using Add watch option.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Fetch text field value from multiple account records

    Hi Shahbaaz,

    Please find below code. Now, Im not getting any errors but the value is still not showing in the field.

    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);
                       for (var i = 0; i < results.value.length; i++)
                       {
                          alert("inside for");
                          var flags = results.value[i]["new_testfield"];
                       }
                   }
                   else {
                       console.log(req.statusText);
                   }
           }
    }


    Thanks,

    Jon

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch text field value from multiple account records

    Hi john,

    Please try using api call insted of fetch xml, above code is working perfectly for me.

    Thanks,

    Shahbaaz

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans