Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Fetching Child records along with Parent Recrord Web API

Posted on by 111

Hi Experts,

Is is possible with single web api call to fetch the record along with its child record.

i.e I have to fetch the quote record and its associated quote product records in single web api call.

  • Ahtesham Profile Picture
    Ahtesham 111 on at
    RE: Fetching Child records along with Parent Recrord Web API

    Thanks Clofly

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Fetching Child records along with Parent Recrord Web API

    Hi Ahtesham,

    Yes, it is possible to do it if relationship between parent record and its children record is 1:N.

    As for your example, due to quote <-> products(quote lines) is 1:N, so you can retrieve associated product records of a quote by $expand query.  

    Steps:

    1. Find relationship name in Customization:

    pastedimage1586138302391v1.png

    2. Excute web api with expression:

    $expand=quote_details($select=xx1,xx2)

    Result:

    some fields of parent quote:

    pastedimage1586138502023v2.png

    associated products:

    pastedimage1586138546612v3.png

    execute web api directly in address bar:

    pastedimage1586138626449v4.png

    javascript code which is generated by CRMRestBuilder:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/quotes(5db5d46b-a577-ea11-a811-000d3af421db)?$select=name,_pricelevelid_value,quoteid,quotenumber,_transactioncurrencyid_value&$expand=quote_details($select=extendedamount,priceperunit,productname,quantity)", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var name = result["name"];
                var _pricelevelid_value = result["_pricelevelid_value"];
                var _pricelevelid_value_formatted = result["_pricelevelid_value@OData.Community.Display.V1.FormattedValue"];
                var _pricelevelid_value_lookuplogicalname = result["_pricelevelid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var quoteid = result["quoteid"];
                var quotenumber = result["quotenumber"];
                var _transactioncurrencyid_value = result["_transactioncurrencyid_value"];
                var _transactioncurrencyid_value_formatted = result["_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue"];
                var _transactioncurrencyid_value_lookuplogicalname = result["_transactioncurrencyid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                for (var a = 0; a < result.quote_details.length; a  ) {
                    var quote_details_extendedamount = result.quote_details[a]["extendedamount"];
                    var quote_details_extendedamount_formatted = result.quote_details[a]["extendedamount@OData.Community.Display.V1.FormattedValue"];
                    var quote_details_priceperunit = result.quote_details[a]["priceperunit"];
                    var quote_details_priceperunit_formatted = result.quote_details[a]["priceperunit@OData.Community.Display.V1.FormattedValue"];
                    var quote_details_productname = result.quote_details[a]["productname"];
                    var quote_details_quantity = result.quote_details[a]["quantity"];
                    var quote_details_quantity_formatted = result.quote_details[a]["quantity@OData.Community.Display.V1.FormattedValue"];
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

    pastedimage1586138869901v5.png

    Regards,

    Clofly

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

Product updates

Dynamics 365 release plans