Call workflow through Js

 

Call workflow through Js :

function cancelcasetest()
{
try
{
debugger;

callWorkflow(“BD239CB2-0DBA-4C8E-99BA-BFB599616C05”, Xrm.Page.data.entity.getId(),
function () {
debugger;
alert(“Workflow executed successfully”);
Xrm.Page.data.refresh();
},
function () {
debugger;
alert(“Error executing workflow”);
});
}
catch (ex) {
alert(” Exception in cancelcase test function : ” + ex);
}

}

 

function callWorkflow(workflowId, recordId, successCallback, errorCallback, url)
{
if (url == null)
{
url = Xrm.Page.context.getClientUrl();
}

var request = “<s:Envelope xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’>&#8221; +
“<s:Body>” +
“<Execute xmlns=’http://schemas.microsoft.com/xrm/2011/Contracts/Services&#8217; xmlns:i=’http://www.w3.org/2001/XMLSchema-instance’>&#8221; +
“<request i:type=’b:ExecuteWorkflowRequest’ xmlns:a=’http://schemas.microsoft.com/xrm/2011/Contracts&#8217; xmlns:b=’http://schemas.microsoft.com/crm/2011/Contracts’>&#8221; +
“<a:Parameters xmlns:c=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’>&#8221; +
“<a:KeyValuePairOfstringanyType>” +
“<c:key>EntityId</c:key>” +
“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>&#8221; + recordId + “</c:value>” +
“</a:KeyValuePairOfstringanyType>” +
“<a:KeyValuePairOfstringanyType>” +
“<c:key>WorkflowId</c:key>” +
“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>&#8221; + workflowId + “</c:value>” +
“</a:KeyValuePairOfstringanyType>” +
“</a:Parameters>” +
“<a:RequestId i:nil=’true’ />” +
“<a:RequestName>ExecuteWorkflow</a:RequestName>” +
“</request>” +
“</Execute>” +
“</s:Body>” +
“</s:Envelope>”;

var req = new XMLHttpRequest();
req.open(“POST”, url + “/XRMServices/2011/Organization.svc/web”, true);

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&#8221;);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
if (successCallback) {
successCallback();
}
}
else {
if (errorCallback) {
errorCallback();
}
}
}
};

req.send(request);
}

 

 

That’s it Enjoy ๐Ÿ™‚

Advertisements

Checking user has a Particular role

Checking user has a Particular role :

In this article, explaining the logged in user has an ”ย Exide POS Userย ” Security Role or not.

// JavaScript source code
function check() {
 var flag = userroles();
 if (flag == true)
 alert("User has Exide Pos User Role.");
 else
 alert("User has doesn't have Exide Pos User Role.");
}

function userroles() {
 debugger;
 var flag = false;
 var roles = [];
 roles = Xrm.Page.context.getUserRoles();
 var SecurityRoles = [];
 for (var i = 0; i < roles.length; i++) {
 SecurityRoles[i] = getrole(roles[i]);
 if (SecurityRoles[i] == "Exide POS User") {
 flag = true;
 }
 }
 return flag;
}

function getrole(roleid) {
 debugger;
 var security_Names;
 var serverUrl = "/" + Xrm.Page.context.getOrgUniqueName();
 var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
 var ODATA_EntityCollection = "/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'"

var oDataSelect = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection;

var retrieveReq = new XMLHttpRequest();
 retrieveReq.open("GET", oDataSelect, false);
 retrieveReq.setRequestHeader("Accept", "application/json");
 retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
 retrieveReq.onreadystatechange = function () {
 security_Names = GetRoleData(this);
};
 retrieveReq.send();
 return security_Names;
}

function GetRoleData(retrieveReq) {
 debugger;
 if (retrieveReq.readyState == 4) {
 if (retrieveReq.status == 200) {
 var retrieved = JSON.parse(retrieveReq.responseText).d;
 return retrieved.results[0].Name;
 }
 }
}

 

That’s It. Enjoy ๐Ÿ™‚