Hello everyone, in my last blog I have mentioned the process to get workflow GUID in JS and trigger the same WF from JS file. But only problem is that code is browser specific. That means it will work only when we will open CRM application in Internet Explorer. For other browsers (Chrome and Firefox) it will not work.
Earlier code was:
function GetWorkflowId(wfName) {
var serverUrl = Xrm.Page.context.getClientUrl();
//Odata query to fetch the WF Guid by WF name
var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc /WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", odataQuery, false);
xmlHttp.send();
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(result);
return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
}
}
Here Chrome and Firefox does not recognize ActiveXObject object and throws error as “ActiveXObject does not defined.
Then I have tried below code
function GetWorkflowId(wfName) {
try {
var serverUrl = Xrm.Page.context.getClientUrl();
var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", odataQuery, false);
xmlHttp.send();
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText;
var xmlDoc;
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(result);
}
catch (e) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(result, "text/xml");
}
return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
}
}
catch (e) {
alert(e.message);
}
}
But again this code doesn’t support Chrome and throws error but it works on Firefox and IE. But I need a proper working code irrespective of browsers used to run CRM application, so I have changed the code snippet contains “ActiveXObject” with below code
function GetWorkflowId(wfName) {
var serverUrl = Xrm.Page.context.getClientUrl();
//Odata query to fetch the WF Guid by WF name
var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc /WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
requestResults1 = RestCall(odataQuery);
if (requestResults1 != null && requestResults1.results.length == 1) {
var WorkFlow = requestResults1.results[0];
var Wid = WorkFlow.WorkflowId;
return Wid;
}
}
:) :)