CRM 2011 + Updating Lookup values using OData and JavaScript.


In this article I will show you how you can update a lookup field, which is nothing but a related entity field, on an entity in CRM 2011 using OData and JavaScript. Updating a lookup field in OData is not the same as we do it in entity form JavaScript. The following code sample will demonstrate the implementation of this scenario.

var updateAccountReq = new XMLHttpRequest();
var account = new Object();
var contactid = "";

//Updating Lookup field
account.account_parent_contact = {
//Guid of the record to be associated
Id: contactid,
//Logical name of the lookup entity
LogicalName: "contact"
}

updateAccountReq.open("POST", OdataUrl + "/AccountSet(guid'" + businessId + "')", true);
updateAccountReq.setRequestHeader("Accept", "application/json");
updateAccountReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//the "Merge" keyword signifies an update.
//Don’t add this line in case of adding a new entity.
updateAccountReq.setRequestHeader("X-HTTP-Method", "MERGE");
updateAccountReq.onreadystatechange = function () {
//Async call return function.
updateAccountReqCallBack(this);
            };
updateAccountReq.send(JSON.stringify(account));

 
Advertisements

One thought on “CRM 2011 + Updating Lookup values using OData and JavaScript.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s