Archive
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);
🙂
JScript validation on Activation/Deactivation of record in CRM 2011
Hi,
We can perform JScript validation on Activation or Deactivation of a record in CRM 2011.
We can achieve this by reading event save mode from CRM context (i.e.,CRM returns unique Codes or Numbers for different actions; Example 5 for Deactivation button click & 6 for Activation click).
Note :- CRM 2011 JScript allows us to pass “Execution Context” as first parameter to Jscript function.
Below are the steps
1) Create a new Jscript file and place below JScript function
// Use the following function on Form Save Event,
// CRM will pass the execution context in function paramter prmContext
function FrmOnSave(prmContext) {
var wod_SaveMode;
if (prmContext != null && prmContext.getEventArgs() != null) {
// “getSaveMode()” returns event mode value (i.e., 5 on Deactivation button click etc…)
wod_SaveMode = prmContext.getEventArgs().getSaveMode();
// 5 will pass on Deactivate button click
if (wod_SaveMode == 5) {
// Write your “Deactivate” validation code here
alert(“Deactivation event fired”);
}
// 6 will pass on Activate button click
elseif (wod_SaveMode == 6) {
// Write your “Activate” validation code here
alert(“Activation event fired”);
}
// Use the code line below only if validation is failed then abort function save event
prmContext.getEventArgs().preventDefault();
}
}
2. Add the JScript file as a webresource to the default solution
3. Register the function on “onsave” event (As below)
4. Save & Publish
5. Open the record and click on “Activate/Deactivate” button (Refer below screen)
– Below is the useful post on the same
http://social.technet.microsoft.com/wiki/contents/articles/4122.aspx
Hope it helps 🙂
Activate or Deactivate record in CRM 2011
Hi,
We can Activate\Deactivate record using “SetStateRequest“. Below is the code snippet.
private void SetEntityStatus(IOrganizationService service, Guid recordGUID, string entityName) {
SetStateRequest setState = newSetStateRequest();
setState.EntityMoniker = newEntityReference();
//Pass GUID of the record to be activated or Deactivated
setState.EntityMoniker.Id = recordGUID;
setState.EntityMoniker.Name = entityName;
setState.EntityMoniker.LogicalName = entityName;
//Setting ‘State’ i.e., (0 – Active ; 1 – InActive)
setState.State = new OptionSetValue();
setState.State.Value = 0/1;
//Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)
setState.Status = new OptionSetValue();
setState.Status.Value = 1/2;
SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
}
Hope it helps 🙂