Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Looping through Child records using JS

  • 1.  Looping through Child records using JS

    SILVER CONTRIBUTOR
    Posted Jan 09, 2020 10:20 AM
    Hello
    Just wondering if anyone knows how to or has a link to a blog explaining how to (using JS) loop through child records of a contact record, checking the value of a Boolean attribute and then returning True/False if one of the records has the Boolean set true.

    Any advice much appreciated.
    Thanks

    ------------------------------
    James Rees
    Senior Systems Engineer
    Liverpool
    ------------------------------
    Academy - Online Interactive Learning from Experts


  • 2.  RE: Looping through Child records using JS

    SILVER CONTRIBUTOR
    Posted Jan 09, 2020 10:39 AM

    @James Rees 

    I would first recommend the ​CRM REST Builder (https://github.com/jlattimer/CRMRESTBuilder) to help with building the WebAPI calls.

    I  would call the retrieveMultiple records WebAPI call for the entity you are wanting to check and pass reference to the contact, and additionally select the field you are wanting with a select statement. Similar to below. I used the connection entity as an example

    Xrm.WebApi.online.retrieveMultipleRecords("connection", "?$filter=_record1id_value eq GUID_Of_Contact").then(
        function success(results) {
            for (var i = 0; i < results.entities.length; i++) {
                    //Check Bool field
                var connectionid = results.entities[i]["connectionid"];
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );


    Let me know if that helps!



    ------------------------------
    Erik Seitz
    RJ Corman Railroad Group, LLC
    Nicholasville KY
    ------------------------------

    Academy - Online Interactive Learning from Experts


  • 3.  RE: Looping through Child records using JS

    GOLD CONTRIBUTOR
    Posted Jan 09, 2020 10:43 AM
    Hey James,

    You could use a loop function but if you are just looking to see if any of the child records has a true value on a field, the Dynamics API now has a fetchxml query to save you the loops. Here is blog that discusses it: https://www.c-sharpcorner.com/article/execute-fetchxml-with-webapi-in-dynamics-365-using-javascript/. Once you return the value of your fetch you can check if the value is > 0 and develop your next steps from there.

    ------------------------------
    Doug Apicella
    Solution Architect
    Hitachi Solutions America, Ltd.
    Greenville SC
    ------------------------------

    Academy - Online Interactive Learning from Experts


  • 4.  RE: Looping through Child records using JS

    SILVER CONTRIBUTOR
    Posted Jan 09, 2020 10:56 AM
    Brilliant - Thanks both - I will see which works best for me.

    ------------------------------
    James Rees
    Senior Systems Engineer
    Liverpool John Moores University
    Liverpool
    ------------------------------

    Academy - Online Interactive Learning from Experts


  • 5.  RE: Looping through Child records using JS

    TOP CONTRIBUTOR
    Posted Jan 09, 2020 11:09 AM
    James

    Try array.some().​​​
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

    let childRecords = [{checkMe: false}, {checkMe: false}, {checkMe: true}];
    console.log(childRecords.some(cr => cr.checkMe)); //true
    
    childRecords = [{checkMe: false}, {checkMe: false}, {checkMe: false}];
    console.log(childRecords.some(cr => cr.checkMe)); //false​



    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Academy - Online Interactive Learning from Experts


If you've found this thread useful, dive deeper into User Group community content by role