Update record using OData in MS CRM

One of important and difficult thing I have faced was updating record through OData. So I would like to give you a small code snippet which I am using for update.

Ex: Assume that I have to update name and salary of an employee record. So my code snippet for this scenario would be like below.

var updateEmpRecord = new Object();
                updateEmpRecord.fullname = 'Steve Smith';
                updateEmpRecord.new_age = '28';
                var jsonEntity = window.JSON.stringify(updateEmpRecord);
                var empGuid="DCD5DBFB-0666-DC11-A%D9-0003FF9CE217";
                var ODATA_ENDPOINT="http://" + window.parent.location.host + "/" + window.parent.Xrm.Page.context.getOrgUniqueName() + "/XRMServices/2011/OrganizationData.svc/";
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: ODATA_ENDPOINT + "/new_employeeSet" + "(guid'" + empGuid + "')",
                    data: jsonEntity,
                    beforeSend: function (XMLHttpRequest) {
                        //Specifying this header ensures that the results will be returned as JSON.
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                        XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
                        //write your logic
                    },
 
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
 
                    }
 
                });

No comments:

Post a Comment

Featured Post

Improving MS CRM Performance

Performance on MS CRM is always a crucial thing and we may follow different ways to achieve the performance thing. Below is the one more a...