Home » Microsoft TechnologiesRSS

Trigger Workflow from Javascript

Hi all,

I'm using the following JS function to call a WorkFlow from Javascript:

 

 

function TriggerWorkflow() {
  var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    GenerateAuthenticationHeader() +
    "<soap:body>" +
    "<execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +"<request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<entityid>" + crmForm.ObjectId + "</entityid>" +
    "<workflowid>" + WorkflowId + "</workflowid>" + //WorkflowId = guid of the workflow"</request>" +
    "</execute>" +
    "</soap:body>" +
    "</soap:envelope>" +
  "";

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
  xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  xmlHttpRequest.send(xml);

  var resultXml = xmlHttpRequest.responseXML;
  return (resultXml.xml);
}

 

The result is that workflow does not start and looking at CRM Trace Logs it shows this error:

at SoapHeaderAuthenticationProviderBase.Authenticate(HttpApplication application)
at AuthenticationStep.Authenticate(HttpApplication application)
at AuthenticationPipeline.Authenticate(HttpApplication application)
at AuthenticationEngine.Execute(Object sender, EventArgs e)
at SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at ApplicationStepManager.ResumeSteps(Exception error)
at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
>CrmAuthenticationToken is missing.

The WF is set as "OnDemand" and the scope is Organization. I'm logged with System Administrator user role.

Do you have any idea about the problem?

 

Thanks to everyone

 

 

Marco 

 

 

 

 

 

5 Answers Found

 

Answer 1

You may want to inspect the resultant value of the xml variable.  The error  seems to suggest that the GenerateAuthenticationHeader did not properly provide the CrmAuthenticationToken necessary to communicate with the web  service.  Are you executing this script from an IFD environment?
 

Answer 2

Hi David, 

I'm using an on Premise installation of CRM.

var xml  value is:

 

"<?xmlversion="1.0"encoding="utf-8"?><soap:envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><CrmAuthenticationTokenxmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;WebServices"><AuthenticationTypexmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">0</AuthenticationType><CrmTicketxmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes"></CrmTicket><OrganizationNamexmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">EIMSCRM</OrganizationName><CallerIdxmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">00000000-0000-0000-0000-000000000000</CallerId></CrmAuthenticationToken></soap:Header><soap:body><executexmlns="http://schemas.microsoft.com/crm/2007/WebServices"><requestxsi:type="ExecuteWorkflowRequest"><entityid>{242770EC-F9B9-DF11-A643-0800273CAB07}</entityid><workflowid>3ADE6233-2078-4CF1-91C8-21905CF4EE52</workflowid></request></execute></soap:body></soap:envelope>"

 

 

The header section s the same that I can find in another fetchXML call  that works, so it should be correct.

 

Answer 3

I assume you zero'd out the CallerId value for posting purposes.  Perhaps the error  is caused indirectly by some part of the <soap:envelope>, which you seem to implement many of the namespace elements in all lower case.  If I recall, XSL and XML are both case-sensitive, and often require proper capitalization and order of objects.  Look at this post and see how the XML assembled there differs from yours.  Then, try to reconcile the capitalization variances between yours and Jim's.  This may improve the situation.

I suspect that because "envelope" and "Envelope" may be treated differently, that what crm  cannot find is the Envelope->CrmAuthenticationToken, rather than the envelope->CrmAuthenticationToken you have provided.

 

Answer 4

Please encode the entityid using inbuilt escape() javascript  function as follows

escape(crmForm.ObjectId)


Hope this helps. Amar
 

Answer 5

I assume you zero'd out the CallerId value for posting purposes.  Perhaps the error  is caused indirectly by some part of the <soap:envelope>, which you seem to implement many of the namespace elements in all lower case.  If I recall, XSL and xml  are both case-sensitive, and often require proper capitalization and order of objects.  Look at this post and see how the XML assembled there differs from yours.  Then, try to reconcile the capitalization variances between yours and Jim's.  This may improve the situation.

I suspect that because "envelope" and "Envelope" may be treated differently, that what crm  cannot find is the Envelope->CrmAuthenticationToken, rather than the envelope->CrmAuthenticationToken you have provided.


Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com

Now is working!!!!!

The the zero'd callerId is from system  and is correct (I think it means "current user").

The error was that all the xml request used lowercase tags: once corrected all with proper uppercase the request worked.

Here is the code that is actually working:

function TriggerWorkflow() {
  var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    GenerateAuthenticationHeader() +
    "<soap:Body>" +
    "<Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +"<Request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<EntityId>" + crmForm.ObjectId + "</EntityId>" +
    "<WorkflowId>" + WorkflowId + "</WorkflowId>" + //WorkflowId = guid  of the workflow"</Request>" +
    "</Execute>" +
    "</soap:Body>" +
    "</soap:Envelope>";

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
  xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  xmlHttpRequest.send(xml);

  var resultXml = xmlHttpRequest.responseXML;
  return (resultXml.xml);
}

Marco

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Tech Videos   |   Follow us on Twitter