Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Problems Using fetchXml with Xrm.WebApi.retrieveMultipleRecords in Dynamics 365 V9

Posted on by 1,579

I have attempted to use the code example from this This Community Forum Post to retrieve related records, but am not getting the same result in the post. 

My function is shown below:

function CheckEstimateChange(executionContext) {
    var formContext = executionContext.getFormContext();
    var myId = formContext.data.entity.getId();
    var FetchXML = "<fetch>" +
"  <entity name='cpp_estimate' >" +
"    <attribute name='statecode' />" +
"    <attribute name='cpp_estimateid' />" +
"    <attribute name='cpp_jobid' />" +
"    <filter type='or' >" +
"      <condition attribute='statuscode' operator='eq' value='923190001' />" +
"      <condition attribute='statuscode' operator='eq' value='1' />" +
"    </filter>" +
"    <link-entity name='cpp_job' from='cpp_jobid' to='cpp_jobid' link-type='inner' >" +
"      <filter type='and' >" +
"        <condition attribute='cpp_jobid' operator='eq' value='"+ myId +"' />" +
"      </filter>" +
"    </link-entity>" +
"  </entity>" +
"</fetch>";
    debugger;

    FetchXML = "?fetchXml=" + encodeURIComponent(FetchXML);

    Xrm.WebApi.retrieveMultipleRecords("cpp_estimate", FetchXML).then(
        function success(result) {
            for (var RecordsCount = 0; RecordsCount < result.entities.length; RecordsCount++) {
                outputText += result.entities[RecordsCount].id + "\t\t" + result.entities[RecordsCount] + "\n";
            }
            Xrm.Utility.alertDialog(outputText, null);
        },
    function (error) {
        // Handle error conditions
        Xrm.Utility.alertDialog(error.message, null);
    });
}

When I run it, the code does not enter the success callback nor does it enter the failure callback, which is very strange.

The screen shot below shows that it appears to jump out of the function and not execute the Xrm.WebApi.retrieveMultipleRecords command.

CodeSkip.png

Has anyone seen this before? Does anyone have any recommendations or advice on how to correct this issue?

I need to use this code to retrieve the GUID of a related record via front end/UI JavaScript.

*This post is locked for comments

  • Suggested answer
    NODAL Profile Picture
    NODAL 860 on at
    RE: Problems Using fetchXml with Xrm.WebApi.retrieveMultipleRecords in Dynamics 365 V9

    Hi jim,

    In Network Tab of chrome developer tools select "XHR" instead of "JS" to view APIs calls.

    Regards,

    Ketan

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Problems Using fetchXml with Xrm.WebApi.retrieveMultipleRecords in Dynamics 365 V9

    Hi Jim,

    Here is the sample code in JavaScript:

    function CheckEstimateChange(executionContext) {

    var formContext = executionContext.getFormContext();
    var myId = formContext.data.entity.getId(); // if its not work replace with var myId = formContext.data.entity.getId().replace("{","").replace("}","")
    var FetchXML = "<fetch>" +
    " <entity name='cpp_estimate' >" +
    " <attribute name='statecode' />" +
    " <attribute name='cpp_estimateid' />" +
    " <attribute name='cpp_jobid' />" +
    " <filter type='or' >" +
    " <condition attribute='statuscode' operator='eq' value='923190001' />" +
    " <condition attribute='statuscode' operator='eq' value='1' />" +
    " </filter>" +
    " <link-entity name='cpp_job' from='cpp_jobid' to='cpp_jobid' link-type='inner' >" +
    " <filter type='and' >" +
    " <condition attribute='cpp_jobid' operator='eq' value='"+ myId +"' />" +
    " </filter>" +
    " </link-entity>" +
    " </entity>" +
    "</fetch>";
    debugger;

    var encodedFetchXml = encodeURI(FetchXML);

    var queryPath = "/api/data/v9.0/cpp_estimate?fetchXml=" + encodedFetchXml; // should be replaced with the entity name you are trying to query and also update version.

    var requestPath = Xrm.Page.context.getClientUrl() + queryPath;

    var req = new XMLHttpRequest();

    req.open("GET", requestPath, true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    this.onreadystatechange = null;

    if (this.status === 200) {

    var returned = JSON.parse(this.responseText);

    var results = returned.value;

    if (results.length > 0) {

    //---- WRITE LOGIC HERE-----------

    //TODO: Implement logic for handling results as desired

    }

    } else {

    alert(this.statusText);

    }

    }

    };

    req.send();

    }

    Or If you don't want to Change any code of yours please try this line :

    <fetch mapping='logical' version='1.0' output-format='xml-platform' distinct='false'> instead of <fetch>

    Hope this help

    Thanks 

    Ajyendra Singh

    Please Mark as verified if this answer is helpful for you.

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: Problems Using fetchXml with Xrm.WebApi.retrieveMultipleRecords in Dynamics 365 V9

    Replacing the code  var myId = formContext.data.entity.getId()" with " var myId = formContext.data.entity.getId().replace("{","").replace("}","") does not fix the issue.

    The same result occurs where it jumps out of the function and does not execute the Xrm.WebApi.retrieveMultipleRecords command.

    When looking on the Network tab in the dev/debug window, nothing jumps out at me as being problematic unless I am not looking in the right place.

    NetWorkTab2.png

    NetWorkTab.png

  • Suggested answer
    NODAL Profile Picture
    NODAL 860 on at
    RE: Problems Using fetchXml with Xrm.WebApi.retrieveMultipleRecords in Dynamics 365 V9

    Hi, 

    Try removing curly braces around GUID. 

    i.e.. Replace line "var myId = formContext.data.entity.getId()" with " var myId = formContext.data.entity.getId().replace("{","").replace("}","")

    Also try checking network tab in chrome dev tool, it will show you exact error for this webapi call. 

    Regards, 

    Ketan

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans