So, from what I understand (i'm no a pro)
If you are on CRM Online UPdate 1 or 2016 then your examples will work, if you are running an older like CRM 2015 On premise (or less) then you cannot simply update statecode, you need to use "SetStateRequest" of the entity record.
in order to update State code we need to SetStatus through a XML Soap "execute" (instead of normal http web request post)
there is a function called "setState" in the XrmServiceToolkit.js that will do that. but it was returning some warnings for me.
I used bits from there and build a simple (less flexible) function ...
var setState = function (entityName, id, stateCode, statusCode) {
///
///
///
///
var requestType = "Execute"
var soapBody = [
"",
"",
"",
"EntityMoniker",
"",
"", id, "",
"", entityName, "",
"",
"",
"",
"",
"State",
"",
"", stateCode.toString(), "",
"",
"",
"",
"Status",
"",
"", statusCode.toString(), "",
"",
"",
"",
"",
"SetState",
""
].join("");
var soapXml = ["",
"",
"<", requestType, " xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",soapBody, "",
"",
""
].join("");
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() "/XRMServices/2011/Organization.svc/web", false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/" requestType);
req.send(soapXml);
if (req.status === 200) {
return "ok"; //req.responseXML;
} else {
return "error";
}
};