SetStateRequest = function (_entityname, entityid, _state, _status) { var requestMain = "" requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"; requestMain += " <s:Body>"; requestMain += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"; requestMain += " <request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">"; requestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">"; requestMain += " <a:KeyValuePairOfstringanyType>"; requestMain += " <c:key>EntityMoniker</c:key>"; requestMain += " <c:value i:type=\"a:EntityReference\">"; requestMain += " <a:Id>" + entityid + "</a:Id>"; requestMain += " <a:LogicalName>" + _entityname + "</a:LogicalName>"; requestMain += " <a:Name i:nil=\"true\" />"; requestMain += " </c:value>"; requestMain += " </a:KeyValuePairOfstringanyType>"; requestMain += " <a:KeyValuePairOfstringanyType>"; requestMain += " <c:key>State</c:key>"; requestMain += " <c:value i:type=\"a:OptionSetValue\">"; requestMain += " <a:Value>" + _state + "</a:Value>"; requestMain += " </c:value>"; requestMain += " </a:KeyValuePairOfstringanyType>"; requestMain += " <a:KeyValuePairOfstringanyType>"; requestMain += " <c:key>Status</c:key>"; requestMain += " <c:value i:type=\"a:OptionSetValue\">"; requestMain += " <a:Value>" + _status + "</a:Value>"; requestMain += " </c:value>"; requestMain += " </a:KeyValuePairOfstringanyType>"; requestMain += " </a:Parameters>"; requestMain += " <a:RequestId i:nil=\"true\" />"; requestMain += " <a:RequestName>SetState</a:RequestName>"; requestMain += " </request>"; requestMain += " </Execute>"; requestMain += " </s:Body>"; requestMain += "</s:Envelope>"; var req = new XMLHttpRequest(); req.open("POST", _getServerUrl(), false) // Responses will return XML. It isn't possible to return JSON. 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/Execute"); var successCallback = null; var errorCallback = null; req.onreadystatechange = function () { SetStateResponse(req, successCallback, errorCallback); }; req.send(requestMain); } SetStateResponse = function (req, successCallback, errorCallback) { if (req.readyState == 4) { if (req.status == 200) { if (successCallback != null) { successCallback(); } } else { errorCallback(SDK.SAMPLES._getError(req.responseXML)); } } }
Also need below two supportive methods.
_getServerUrl = function () { var OrgServicePath = "/XRMServices/2011/Organization.svc/web"; var serverUrl = ""; if (typeof GetGlobalContext == "function") { var context = GetGlobalContext(); serverUrl = context.getServerUrl(); } else { if (typeof Xrm.Page.context == "object") { serverUrl = Xrm.Page.context.getServerUrl(); } else { throw new Error("Unable to access the server URL"); } } if (serverUrl.match(/\/$/)) { serverUrl = serverUrl.substring(0, serverUrl.length - 1); } return serverUrl + OrgServicePath; } _getError = function (faultXml) { var errorMessage = "Unknown Error (Unable to parse the fault)"; if (typeof faultXml == "object") { try { var bodyNode = faultXml.firstChild.firstChild; //Retrieve the fault node for (var i = 0; i < bodyNode.childNodes.length; i++) { var node = bodyNode.childNodes[i]; if ("s:Fault" == node.nodeName) { for (var j = 0; j < node.childNodes.length; j++) { var faultStringNode = node.childNodes[j]; if ("faultstring" == faultStringNode.nodeName) { errorMessage = faultStringNode.text; break; } } break; } } } catch (e) { }; } return new Error(errorMessage); }
Few examples I tried;
Deactivating an account -> SetStateRequest("account", _accountid, 1, 2)
Changing a Salesorder to submitted state -> SetStateRequest("salesorder", _salesorderid, 1, 3)
Disqualifying a lead with reason "No longer interested" -> SetStateRequest("lead", _leadid, 2, 6)
Anyway, still I don’t know whether this works for all the entities... but so far so good :-)
Thank you for this code..it worked for me..I set order status as "Cancelled" through this code.
ReplyDeleteYou are most welcome.. Also thanks for sharing your results here.
DeleteHi, Quick question don't know what I am missing but I get an error saying SDK is not defined. Any pointers?
ReplyDeletePlease modify SetStateResponse Method.
DeleteChange line
errorCallback(SDK.SAMPLES._getError(req.responseXML));
to
errorCallback(_getError(req.responseXML));
Hi - Thanks for this it looks just what I need. I got the SDK not defined error and then made the code change as above but now get an "Object Expected" error at this line. I commented the line out and it seems to work but the state/status does not seem to change until the record is saved. Is that what you would expect? The entity I am changing is a custom entity - not sure if matters. Any help much appreciated - thanks
DeleteHi Nick,
DeleteI really cant figure out your issue in this point and time. Are you sure that the same event is not doing any other tasks which affect this? Also _getError() method can be referenced from the code?
Only other explanation would be that recent rollups. I actually havent used this with most recent rollups of CRM. Next best bet would be this (http://xrmservicetoolkit.codeplex.com/)javascript library. Sorry for not been very helpful..
Hi Nick,
DeleteI'm having the same issue. Do you ever figure this out?
Thank you!
Hii Sumedha , got Stuck at same place as mentioned by Nick and Beth. Will be helpful if you find it out.
DeleteThis comment has been removed by the author.
ReplyDeleteHii I want to make entity FROM read only if record status change other wise it should be editable
ReplyDelete