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)

odata query

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I am working on a oData query which returns data that I want when I run it in odata query designer tool.

But when I use the same in JS and debug, the readyState shows 1 instead of 4 and comes out of the function. Could someone please look at this script and let me know if I am doing anything wrong?

function Contact()
{
var Name=Xrm.Page.getAttribute("new_lapnew").getValue();

if (Name != null) {
         var Id = Name[0].id;
     }

var serverUrl=Xrm.Page.context.getClientUrl();
alert(serverUrl);
    var oDataSelect=serverUrl+"/XRMServices/2011/OrganizationData.svc/new_lapSet?$select=new_EmailLAP,new_name,new_PrimaryPhoneLAP&$filter=new_lapId eq guid'"+Id+"'";

debugger;
    var retrieveReq=new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
    if (retrieveReq.readyState === 4) {
            if (retrieveReq.status === 200) {

                var retrieved = JSON.parse(retrieveReq.responseText).d;
				
				var email=retrieved.new_EmailLAP;
				var phone=retrieved.new_PrimaryPhoneLAP;
				
				Xrm.Page.getAttribute("new_EmailLAP").setValue(email);
				Xrm.Page.getAttribute("new_PrimaryPhoneLAP").setValue(phone);
				
				return;
			}
        } 
    };
    retrieveReq.send();
}


 

Thanks

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Royal King Profile Picture
    27,686 on at

    you have to wait until ready state changes to 4. Whenever value of ready state changes it calls the function ,so wait for few seconds until ready state reaches 4 to get into your actual code.

    readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:

    0: request not initialized

    1: server connection established

    2: request received

    3: processing request

    4: request finished and response is ready

  • crmprogrammer2013 Profile Picture
    2,665 on at

    That makes sense.

    But I want to auto populate the field based on the query result set. If it takes time, I will not able to populate the other field values as soon as field OnChange event occurs.

    Also, if I debug step by step it is populating the fields but if I don;t debug and just change the field to trigger OnChange, the fields are not auto populating.

    What should I be doing in this scenario?

    Thank you.

  • Royal King Profile Picture
    27,686 on at

    either your request takes lot of time to retrieve the data from the server or something is not correct in the code. You can just add alert to verify whether it hits the point where you recieve the result in your script , f you dont see alert message that means something going wrong in your code

  • Verified answer
    crmprogrammer2013 Profile Picture
    2,665 on at

    Okay, got it. Obviously, a small mistake in my script.

    I took off the extra column which I don't need in the oData query and also I wasn't checking for the nulls and also assigned the two variables like:

                                   var email=retrieved.results[0].new_EmailLAP;

    var phone=retrieved.results[0].new_PrimaryPhoneLAP;

    And it worked.

    Thanks

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