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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript GET request is not retrieving records using Web.Api

(0) ShareShare
ReportReport
Posted on by 804

Hi

I have written following JS function on Incident form to retrieve Contract Lines, but function is not doing anything. I have verified the Fetch Query and it returns results. So data is definitely there. I have debugged it and looks like "this.readyState == 4" is false.

Can anyone please suggest me what is wrong with my code. Do I need to add any assemblies?

Thanks

function Test() {

        var customerId = Xrm.Page.getAttribute("parentcustomer").getValue();
        if (customerId == null) {
            return;
        }

        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
            "<entity name='contractdetail'>" +
            "<attribute name='contractid' />" +
            "<attribute name='contractdetailid' />" +
            "<filter type='and'>" +
            "<condition attribute='statuscode' operator='in'>" +
            "<value>2</value>" +
            "<value>1</value>" +
            "</condition>" +
            "<condition attribute='customerid' operator='eq' value='" +
            customerId[0].id +
            "' />" +
            "</filter>" +           
            "</entity>" +
            "</fetch>";

        var uri = "/contractdetail?fetchXml=" + encodeURIComponent(fetchXml);
		var clientUrl = Xrm.Page.context.getClientUrl();
		var webAPIPath = "/api/data/v8.1";   
		uri = clientUrl + webAPIPath + uri;
      
        var request = new XMLHttpRequest();
        request.open("GET", encodeURI(uri), false);
        request.setRequestHeader("Accept", "application/json");
        request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        request.setRequestHeader("OData-MaxVersion", "4.0");
        request.setRequestHeader("OData-Version", "4.0");
        request.onreadystatechange = function() {
            if (this.readyState == 4 /* complete */) {
                request.onreadystatechange = null;
				
				switch (this.status) {
                    case 200: // Success with content returned in response body.
                    case 204: // Success with no content returned in response body.
                        var data = JSON.parse(this.response);
						if (data && data.value) {
                        for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                            alert(data.value[indexContractLine].contractdetailid);
                            //alert(data.value[indexContractLine]['@odata.etag']);
                        }
                    }
                        break;
                    default: // All other statuses are unexpected so are treated like errors.
                        var error;
                        try {
                            error = JSON.parse(request.response).error;
                        } catch (e) {
                            error = new Error("Unexpected Error");
                        }
                        alert(error);
                        break;
                }
				
                if (this.status == 200) {
                    var data = JSON.parse(this.response);
                    if (data && data.value) {
                        for (var indexContractLine = 0; indexContractLine < data.value.length; indexContractLine++) {
                            alert(data.value[indexContractLine].contractdetailid);
                            alert(data.value[indexContractLine]['@odata.etag']);
                        }
                    } else {
                        var error = JSON.parse(this.response).error;
                        alert(error.message);
                    }
                }
            };
            request.send();
        }
    }	

*This post is locked for comments

I have the same question (0)
  • C. Jensen Profile Picture
    384 on at

    Hi

    I had the same issue once. I solved it by changing the open request to "request.open("GET", encodeURI(uri), true);", so that the reqeust is sent asynchronously.

  • Verified answer
    Hayer Profile Picture
    804 on at

    I was referring to wrong  version Api however the Organization is still on v8.0

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans