SetState Request Example Using Javascript

Add the below Functions in Javascript Webresource and be sure to pass correct StateCode and StatusCode for the particular entity.

function callRecordStatus() {
var recordGuid = Xrm.Page.data.entity.getId();
   //StateCode   2 - Disqualified
   //StatusCode 4 - Lost
setRecordStatus ("lead", recordGuid, "2", "4");
}

function setRecordStatus(entitySchemaName, recordGuid, stateCode, statusCode) {
  // create the request
var request = "";
request += "";
request += "";
request += "";
request += "";
request += "";
request += "EntityMoniker";
request += "";
request += "" + recordGuid +"";
request += "" + entitySchemaName + "";
request += "";
request += "";
request += "";
request += "";
request += "State";
request += "";
request += "" + stateCode + "";
request += "";
request += "";
request += "";
request += "Status";
request += "";
request += "" + statusCode + "";
request += "";
request += "";
request += "";
request += "";
request += "SetState";
request += "";
request += "";
request += "";
request += "";
   //send set state request  
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web",
data: request,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function(data, textStatus, XmlHttpRequest) {
//Add code after changing Status of the record
},

error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}

Note:
Xrm.Page.context.getClientUrl() – New javascript function in CRM 2013 used to get ServerURL.

Please provide your valuable comments on this article.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s