Sunday, 8 March 2015

21:55 - No comments

Update a Record in CRM using OData Query | MS CRM




To Update a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path

Now add the following code to your web resource.
Also add JSon.js and Jquery1.4min.js to the Library
------------------>

function update()
{
var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();   //getting the id through lookup
var id = lookupObject[0].id;
var obj = new Object();
obj.new_name = "Hello";
var set="your entity schema nameSet"
updatelog(id,obj,set);

}
function updatelog(id,obj1,odata)
{
var jsonEntity = window.JSON.stringify(obj1);
var serverUrl = window.parent.Xrm.Page.context.getClientUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
$.ajax({
                                type: "POST",
                                contentType: "application/json; charset=utf-8",
                                datatype: "json",
                                data: jsonEntity,
                                url: serverUrl + ODATA_ENDPOINT + "/" + odata + "(guid'" + id + "')",
                                beforeSend: function(XMLHttpRequest)
                                {
                                                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                                                XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        }
                                success: function (data, textStatus, XmlHttpRequest) {
                                alert("Updated successfully");
                                },
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                if (XmlHttpRequest && XmlHttpRequest.responseText) {
                                alert("Error while updating " + odataSetName+ " ; Error – " + XmlHttpRequest.responseText);
                                }
                                }
                });

}

0 comments:

Post a Comment