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));
Thanks! Works great.