Associate records in MS CRM 5

Following is the sample code to create the relationship by using URI. I tried to explore on web and there's no one posted an example for associating records in MS CRM 2011. Finally I found it bu myself.

parentId = parentId.replace("{", "").replace("}", "");
childId = childId.replace("{", "").replace("}", "");

var odataUri = serverUrl + ODATA_ENDPOINT + "/" + parentOdataSet + "(guid'" + parentId + "')/$links/" + relationName;

var entityObject = new Object();
entityObject.uri = serverUrl + ODATA_ENDPOINT + "/" + childOdataSet + "(guid'" + childId + "')";

var jsonEntity = window.JSON.stringify(entityObject);

var result = null;
var option = {
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: odataUri,
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
else {
result = data.d;
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback)
errorCallback(XmlHttpRequest, textStatus, errorThrown);
else {
result = new Object();
result.XmlHttpRequest = XmlHttpRequest;
result.textStatus = textStatus;
result.errorThrown = errorThrown;
}
},
async: (successCallback != null)
};

$.ajax(option);

Comments