Sunday, June 26, 2011

Update Money Field Using REST API

I saw few questions about this subject as it is not included in the SDK examples and not very trivial for REST beginners. The key thing to understand is that the MS CRM money field is an object. So not like string or integer, you need to correctly update the object value.
For our example, lets assume we have a campaign record and we want to update the "OtherCost" field. Here is how it is done:


// update the parent campaign with the total value
var changes = { "OtherCost": { "Value": total.toFixed(2)} };

var updateReq = new XMLHttpRequest();
var target = context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/CampaignSet(guid'" + campaignId + "')";
updateReq.open("POST", target, true);
updateReq.setRequestHeader("Accept", "application/json");
updateReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
updateReq.setRequestHeader("X-HTTP-Method", "MERGE");

updateReq.onreadystatechange = function () {
if (updateReq.readyState == 4 /* complete */) {
    if (updateReq.status == 204 || updateReq.status == 1223) {
       // success
    }
    else
    {
       //OMG an error occured! Do something!
    }
}


};
updateReq.send(JSON.stringify(changes));

No comments:

Post a Comment