web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 1,589

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

I have the same question (0)
  • Suggested answer
    NODAL Profile Picture
    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

  • ACECORP Profile Picture
    1,589 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

  • ajyendra Profile Picture
    1,738 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.

  • Suggested answer
    NODAL Profile Picture
    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans