CRM 2011 Update Using the REST Endpoint with JavaScript fails

510 Views Asked by At

In MS Dynamics CRM 2011 (which is new to me, I haven't worked with it prior to a few weeks ago), I'm trying to update a custom entity field value from the onsave event of a form. Here's the script saved in the WebResource attached to the form and the onsave event there (_orgDataPath is retrieved from the page context and works e.g. for a Retrieve request on the same form, so it does not appear to be the source of the issue here):

function updateRecord(id, type) {  
        var object = { new_CurrentIndex: 12345 };
        var req = new XMLHttpRequest();
        req.open("POST", encodeURI(this._orgDataPath() + type + "Set(guid'" + id + "')"), false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("X-HTTP-Method", "MERGE");

        var jsonEntity = window.JSON.stringify(object);

        req.send(jsonEntity);
}

When I run this in the form's onsave event, the new_CurrentIndex value of the custom entity is not updated.

So my questions are: 1) Is the code above correct for a synchronous REST update using javascript? 2) If the code for the basic REST update above is correct, are there CRM configuration settings that would prevent the update from succeeding?

1

There are 1 best solutions below

0
On

f12 will open developer tools - use the debug & step through your function as its called to see where you have gone wrong