Advertisements
Home > CRM, CRM 2011, JScript > Activate or Deactivate record using jscript

Activate or Deactivate record using jscript

Hi,

Below is the script to Activate or Deactivate record using jscript.

// ‘State’  – Activate 0; InActive 1

// ‘Status’ – Active 1; InActive 2

function ActivateOrDeactivate(entityName, entityId, status, state) {

var setStateRequest = ‘<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/“>’ +

‘<s:Body>’ +

‘<Execute xmlns=”http://schemas.microsoft.com/xrm/2011/Contracts/Services” xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“>’ +

‘<request i:type=”a:UpdateRequest” xmlns:a=”http://schemas.microsoft.com/xrm/2011/Contracts“>’ +

‘<a:Parameters xmlns:b=”http://schemas.datacontract.org/2004/07/System.Collections.Generic“>’ +

‘<a:KeyValuePairOfstringanyType>’ +

‘<b:key>EntityMoniker</b:key>’ +

‘<b:value i:type=”a:EntityReference”>’ +

‘<a:Id>’ + entityId + ‘</a:Id>’ +

‘<a:LogicalName>’ + entityName + ‘</a:LogicalName>’ +

‘<a:Name i:nil=”true”></a:Name>’ +

‘</b:value>’ +

‘</a:KeyValuePairOfstringanyType>’ +

‘<a:KeyValuePairOfstringanyType>’ +

‘<b:key>State</b:key>’ +

‘<b:value i:type=”a:OptionSetValue”>’ +

‘<a:Value>’ + state + ‘</a:Value>’ +

‘</b:value>’ +

‘</a:KeyValuePairOfstringanyType>’ +

‘<a:KeyValuePairOfstringanyType>’ +

‘<b:key>Status</b:key>’ +

‘<b:value i:type=”a:OptionSetValue”>’ +

‘<a:Value>’ + status + ‘</a:Value>’ +

‘</b:value>’ +

‘</a:KeyValuePairOfstringanyType>’ +

‘</a:Parameters>’ +

‘<a:RequestId i:nil=”true”></a:RequestId>’ +

‘<a:RequestName>SetState</a:RequestName>’ +

‘</request>’ +

‘</Execute>’ +

‘</s:Body>’ +

‘</s:Envelope>’;

var req = new XMLHttpRequest();

var serverUrl = Xrm.Page.context.getServerUrl();

req.open(“POST”, serverUrl, false);

req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute“);

req.send(setEntityStateRequest);

if (req.status != 200) {

alert(“Error while set status – “+req.responseXML);

}

}

How Do I call this method :-

  • Below is the sample to activate “Contact” record

var contactId={contact GUID};

ActivateOrDeactivate(“contact”, contactId, 0, 1);

🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: