locked
Trigger Workflow from Javascript RRS feed

  • Question

  • 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 

     

     

     

     

    Thursday, September 9, 2010 2:56 PM

Answers

  • 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
    • Proposed as answer by Amarsen Vangoor Saturday, September 11, 2010 7:15 AM
    • Marked as answer by Jim Glass Jr Monday, September 13, 2010 3:23 PM
    Friday, September 10, 2010 6:51 PM
    Moderator
  • 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

    • Marked as answer by Marco Dodaro Thursday, September 16, 2010 10:36 AM
    Thursday, September 16, 2010 10:35 AM

All replies

  • 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?
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Thursday, September 9, 2010 4:56 PM
    Moderator
  • Hi David, 

    I'm using an on Premise installation of CRM.

    var xml value is:

     

    "<?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">
    	<soap:Header>
    		<CrmAuthenticationToken xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;WebServices">
    			<AuthenticationType xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">0</AuthenticationType>
    			<CrmTicket xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes"></CrmTicket>
    			<OrganizationName xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">EIMSCRM</OrganizationName>
    			<CallerId xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>
    		</CrmAuthenticationToken>
    	</soap:Header>
    	<soap:body>
    		<execute xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
    			<request xsi: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.

    Friday, September 10, 2010 10:21 AM
  • 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
    • Proposed as answer by Amarsen Vangoor Saturday, September 11, 2010 7:15 AM
    • Marked as answer by Jim Glass Jr Monday, September 13, 2010 3:23 PM
    Friday, September 10, 2010 6:51 PM
    Moderator
  • Please encode the entityid using inbuilt escape() javascript function as follows

    escape(crmForm.ObjectId)


    Hope this helps. Amar
    Saturday, September 11, 2010 7:27 AM
  • 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

    • Marked as answer by Marco Dodaro Thursday, September 16, 2010 10:36 AM
    Thursday, September 16, 2010 10:35 AM