Tag Archives: REST

Working With REST Results in CRM 2011

A short post today.   When using Microsoft CRM 2011’s REST oData End Point to create a new record or to retrieve an existing record the result is provided to you in 2 different ways.  This can catch you out.

Retrieving Records

When querying for existing records the result provided by the oData query is an array of CRM records.  You can access that by referring to data.d.results[0] as illustrated below:

success: function (data, textStatus, XmlHttpRequest) {
	var Contact = data.d.results[0];
	if (Contact.Telephone1 != null) {
		Xrm.Page.data.entity.attributes.get("new_phonenumber2").setValue(Contact.Telephone1);
		alert("phone set");
	}

I’m placing the first CRM object in that array into a variable called Contact.  In the above example my query (which I haven’t included) was performing a top1 selection so I am only expecting one record and therefore only interested in the first record (array value 0). 

I then retrieve the Contact’s phone number by referring to Contact.Telephone1 where Contact is my object variable and Telephone1 is one of it’s attributes.  To know the attribute name and the correct case to use make sure you test your REST query in IE or using the oData Query Designer.

Creating Records

When creating a new record you will be returned the object that was created.  This will be just a single CRM object rather than an array of objects.  You can access that by referring to data.[“d”] as illustrated below:

success: function (data, textStatus, XmlHttpRequest) {
	var NewCRMRecordCreated = data["d"];
	alert(NewCRMRecordCreated.IncidentId);

Hope this helps.  Smile