Link to home
Start Free TrialLog in
Avatar of Bakersville
BakersvilleFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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;
	
	//Replace{}
	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" + 
  "&$top=1";
  
    alert(oDataPath);
	
  alert(filter);
  
  //Now collect information together
  
	var retrieveRecordsReq = new XMLHttpRequest();
	retrieveRecordsReq.open("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");
			alert(DebtDetails.dar_debtdetailsmatterId);
			alert(DebtDetails.dar_debtdetailsmatter);
           }
       }
	   else
	   {
	   alert(this.status);
	   }
    }
	else
	{
	alert(this.readyState);
	}
};
retrieveRecordsReq.send();

}

Open in new window


Please can anyone help.
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

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-

    $.ajax({
        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 https://xrmservicetoolkit.codeplex.com
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 - http://xrmservicetoolkit.codeplex.com/wikipage?title=Rest%20Functions

Why don't you go for simple SOAP call instead? Check this- http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions
ASKER CERTIFIED SOLUTION
Avatar of Bakersville
Bakersville
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bakersville

ASKER

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);

returnd

dar_debtdetailmatter, undefined, undefined, undefined

Can anyone help please as I am loosing the will

Thank you
Hi,

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?