This post describes the way to update the entity for all different type of fields in the entity
As shown in the sample below it creates a new account with different set of types
var account = {};
account.Name = "Test Account Name";
account.Description = "This account was created by the JQueryRESTDataOperations sample.";
if (primaryContact != null) {
//Set a lookup value
account.PrimaryContactId = { Id: primaryContact.ContactId, LogicalName: "contact", Name: primaryContact.FullName };
}
//Set a picklist value
account.PreferredContactMethodCode = { Value: 2 }; //E-mail
//Set a money value
account.Revenue = { Value: "2000000.00" }; //Set Annual Revenue
//Set a Boolean value
account.DoNotPhone = true; //Do Not Allow
var jsonEntity = window.JSON.stringify(account);
var serverUrl = Xrm.Page.context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/AccountSet";
var ODataPath = serverUrl + ODATA_ENDPOINT;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODataPath + "(guid'" + accountId + "')",
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
}
});
If you observe the about code the following statement defines whether the ODATA call is for update or insert
For Update
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
For Delete
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
For Insert
XMLHttpRequest.setRequestHeader("Accept", "application/json");
No comments:
Post a Comment