Pages

Monday 10 March 2014

Update a Record in CRM using OData Query

Hi Folks,

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
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
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 entity = new Object();
    entity.new_name="Hello";
    var set="your entity schema nameSet";              //entity name with Set
    updateRecord(id, entity, set);
}
    function updateRecord(id, entityObject, odataSetName) {
    var jsonEntity = window.JSON.stringify(entityObject);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(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);
    }
    }
    });
    }

----------------------->

No comments:

Post a Comment