readystate=400 crm 2011 when retreiveing lookup from another entity

Hi everyone

I am having some issues when I am trying to collect a lookup field from one entity to put onto my newly created form.

It all works up to this.readystate. This always = 400 and I am unable to figure out why.

Please see code below

///<reference path="Libraries\jquery-1.4.4.js" />
///<reference path="Libraries\AjaxREST.js" />
///<reference path="Libraries\json.js" />

function SetDebtDetails() {
//Get the ID for Matter
	var matterItem = Xrm.Page.getAttribute("dar_matter").getValue();
	var matterID = matterItem[0].id;
	matterID = matterID.replace('{', '').replace('}', '');
	//Get ODataPath
	var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
	//Construct the OData query
	var filter = "/IncidentSet?" +
  "$select=dar_debtdetailsmatter" + 
  "&$filter=dar_matter/Id eq (guid'" + matterID + "')" + 
  "&$orderby=dar_matter desc" + 
  //Now collect information together
	var retrieveRecordsReq = new XMLHttpRequest();"GET", oDataPath + filter, true);
	retrieveRecordsReq.setRequestHeader("Accept", "application/json");
	retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	retrieveRecordsReq.onreadystatechange = function () {
    if (this.readyState == 4) {
       if (this.status == 200) {
           var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d;
           if(retrievedRecords.results.length > 0)
            alert("Going to set lookup");
			var DebtDetails = retrievedRecords.results[0];
            SetLookup("dar_debtdetailsmatter", DebtDetails.dar_debtdetailsmatterId, DebtDetails.dar_debtdetailsmatter, "dar_debtdetailsmatter");
			alert("Set lookup");


Open in new window

Please can anyone help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

I don't think there is any problem in your script. However, the "may be" situation is matterID can be null. But why don't you try other ways which are easily available?!?

Try to use Xrm.Page.context.getClientUrl() instead of getServerUrl() method.

Have you tried requesting it through JQuery? Similar example is below-

        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet",
        data: jsonAccount,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        success: function (data, textStatus, XmlHttpRequest) {
            account = data.d;
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            errorHandler(XMLHttpRequest, textStatus, errorThrown);

Open in new window

However, I would suggest you work with
The library has all the methods ready with it and you just have to call them with parameters. And the return value is the object which can be easily worked within JS.

Here is a help with all methods -

Why don't you go for simple SOAP call instead? Check this-
BakersvilleAuthor Commented:
Hi guys

Thank you for your replies.

I have found my issue with the 400 error, I was using "$select=dar_debtdetailsMatter" and not "$select=dar_debtdetails"

Now I have got rid of the 400 error, it now does not go past readystate 1. I have put an alert passed the retrieveRecordsReq



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BakersvilleAuthor Commented:
hi all

I have finally got over the 400 error, however, I am continually getting an undefined error when I try and break down my results back.

alert("dar_debtdetailmatter", DebtDetails.dar_debtdetailmatterId, DebtDetails.dar_Name, DebtDetails.LogicalName);


dar_debtdetailmatter, undefined, undefined, undefined

Can anyone help please as I am loosing the will

Thank you
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

You should use this-

alert("dar_debtdetailmatter" + DebtDetails.dar_debtdetailmatterId + DebtDetails.dar_Name + DebtDetails.LogicalName);

The alert() method only takes 1 argument. So concatenating all the required properties.

Makes sense?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.