locked
trigger workflow with javascript RRS feed

  • Question

  • Hi all.

    I know this has been asked several times on the forums so please forgive me for asking again.  My issue is that from work I am unable to view most of the referenced blogs.

    So my problem is:  How do I trigger a workflow using javascript?


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 9:54 AM

Answers

  • Hi,

    I have another code for you:

    function callworkfow(id) {
        try {
            var guid;
     
            if (id == null) {
                guid = Xrm.Page.data.entity.getId();
            }
            else
                guid = id;
     
            // assign workflow guid
            var WorkflowId = "89BA3166-948E-4B12-8A55-4BE01DEDAB0B";
     
            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\">" +
        Xrm.Page.context.getAuthenticationHeader() +
        "<soap:Body>" +
        "<execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "<request xsi:type=\"ExecuteWorkflowRequest\">" +
        "<entityid>" + guid + "</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;
     
            if (id == null)
                alert("Success");
            return (resultXml.xml);
        }
        catch (e) {
            alert("Failed to Execute");
        }
    } 

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 10:02 AM
  • Hi

    Many thanx to Ashish and Dimaz.  Should anyone ever come across this thread, here is the final code:

    function callworkflow() {
          
            var WorkflowId = "FD140AAF-4DF4-11DD-BD17-0019B9312238";
    
            /*Generate Soap Body.*/
            var soapBody = "<soap:Body>" +
                     "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
                     "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
                     "      <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
                     "      <WorkflowId>" + WorkflowId + "</WorkflowId>" +
                     "    </Request>" +
                     "  </Execute>" +
                     "</soap:Body>";
    
            /*Wrap the Soap Body in a soap:Envelope.*/
            var soapXml = "<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() +
                    soapBody +
                    "</soap:Envelope>";
    
            /* Create the XMLHTTP object for the execute method.*/
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
            xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    
            /* Send the XMLHTTP object. */
            xmlhttp.send(soapXml);
            
    }


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 2:23 PM

All replies

  • Hi,

    Please find the answer:

    Executing the Workflow using Javascript in MS CRM 2011

    function CheckAccept()
    {
    Xrm.Page.getAttribute('new_filestatus').setValue(1);
    TriggerWorkflow('pat_form1','7BA7F891-64D5-4414-894D-8698FF66F85E');
    Xrm.Page.data.entity.save();
    }
    function TriggerWorkflow(entityName,workflowGuid) {
    /*Generate Soap Body.*/
    var soapBody = "" +
    " " +
    " " +
    " " + Xrm.Page.data.entity.getId() + "" +
    " " + workflowGuid + "" +
    " " +
    " " +
    "";
    /*Wrap the Soap Body in a soap:Envelope.*/

    var soapXml = "

    " 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() +
    soapBody +
    "";
    /* Create the XMLHTTP object for the execute method.*/
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    /* Send the XMLHTTP object. */
    xmlhttp.send(soapXml);
    }
    Call this method like
    TriggerWorkflow('entityname','7BA7F891-64D5-4414-894D-8698FF66F85E');

    It will work.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 9:59 AM
  • Hi,

    I am attaching the same code in the code window:

    Executing the Workflow using Javascript in MS CRM 2011
    function CheckAccept()
    {
    Xrm.Page.getAttribute('new_filestatus').setValue(1);
    TriggerWorkflow('pat_form1','7BA7F891-64D5-4414-894D-8698FF66F85E');
    Xrm.Page.data.entity.save();
    }
    function TriggerWorkflow(entityName,workflowGuid) {
    /*Generate Soap Body.*/
    var soapBody = "" +
    " " +
    " " +
    " " + Xrm.Page.data.entity.getId() + "" +
    " " + workflowGuid + "" +
    " " +
    " " +
    "";
    /*Wrap the Soap Body in a soap:Envelope.*/
    
    var soapXml = "
    " 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() +
    soapBody +
    "";
    /* Create the XMLHTTP object for the execute method.*/
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    /* Send the XMLHTTP object. */
    xmlhttp.send(soapXml);
    }
    Call this method like
    TriggerWorkflow('entityname','7BA7F891-64D5-4414-894D-8698FF66F85E');
    
    It will work.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 10:01 AM
  • Hello,

    Here is answer:

    if (typeof (SDK) == "undefined")
       { SDK = { __namespace: true }; }
           //This will establish a more unique namespace for functions in this library. This will reduce the 
           // potential for functions to be overwritten due to a duplicate name when the library is loaded.
           SDK.SAMPLES = {
               _getServerUrl: function () {
                   ///<summary>
                   /// Returns the URL for the SOAP endpoint using the context information available in the form
                   /// or HTML Web resource.
                   ///</summary>
                   var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
                   var serverUrl = "";
                   if (typeof GetGlobalContext == "function") {
                       var context = GetGlobalContext();
                       serverUrl = context.getServerUrl();
                   }
                   else {
                       if (typeof Xrm.Page.context == "object") {
                             serverUrl = Xrm.Page.context.getServerUrl();
                       }
                       else
                       { throw new Error("Unable to access the server URL"); }
                       }
                      if (serverUrl.match(/\/$/)) {
                           serverUrl = serverUrl.substring(0, serverUrl.length - 1);
                       } 
                       return serverUrl + OrgServicePath;
                   }, 
               ExecuteWorkflowRequest: function () {
                   var requestMain = ""
                   requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
                   requestMain += "  <s:Body>";
                   requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                   requestMain += "      <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
                   requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
                   requestMain += "          <a:KeyValuePairOfstringanyType>";
                   requestMain += "            <c:key>EntityId</c:key>";
                   requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">a46fa1c1-e38d-e011-86ba-1cc1dee8ea49</c:value>";
                   requestMain += "          </a:KeyValuePairOfstringanyType>";
                   requestMain += "          <a:KeyValuePairOfstringanyType>";
                   requestMain += "            <c:key>WorkflowId</c:key>";
                   requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">d009c04f-f826-4b3b-90cd-209581cfc2ff</c:value>";
                   requestMain += "          </a:KeyValuePairOfstringanyType>";
                   requestMain += "        </a:Parameters>";
                   requestMain += "        <a:RequestId i:nil=\"true\" />";
                   requestMain += "        <a:RequestName>ExecuteWorkflow</a:RequestName>";
                   requestMain += "      </request>";
                   requestMain += "    </Execute>";
                   requestMain += "  </s:Body>";
                   requestMain += "</s:Envelope>";
                   var req = new XMLHttpRequest();
                   req.open("POST", SDK.SAMPLES._getServerUrl(), true)
                   // Responses will return XML. It isn't possible to return JSON.
                   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");
                   var successCallback = null;
                   var errorCallback = null;
                   req.onreadystatechange = function () { SDK.SAMPLES.ExecuteWorkflowResponse(req, successCallback, errorCallback); };
                   req.send(requestMain);
               },
           ExecuteWorkflowResponse: function (req, successCallback, errorCallback) {
                   ///<summary>
                   /// Recieves the assign response
                   ///</summary>
                   ///<param name="req" Type="XMLHttpRequest">
                   /// The XMLHttpRequest response
                   ///</param>
                   ///<param name="successCallback" Type="Function">
                   /// The function to perform when an successfult response is returned.
                   /// For this message no data is returned so a success callback is not really necessary.
                   ///</param>
                   ///<param name="errorCallback" Type="Function">
                   /// The function to perform when an error is returned.
                   /// This function accepts a JScript error returned by the _getError function
                   ///</param>
                   if (req.readyState == 4) {
                   if (req.status == 200) {
                   if (successCallback != null)
                   { successCallback(); }
                   }
                   else {
                       errorCallback(SDK.SAMPLES._getError(req.responseXML));
                   }
               }
           },
           _getError: function (faultXml) {
               ///<summary>
               /// Parses the WCF fault returned in the event of an error.
               ///</summary>
               ///<param name="faultXml" Type="XML">
               /// The responseXML property of the XMLHttpRequest response.
               ///</param>
               var errorMessage = "Unknown Error (Unable to parse the fault)";
               if (typeof faultXml == "object") {
                   try {
                       var bodyNode = faultXml.firstChild.firstChild;
                       //Retrieve the fault node
                       for (var i = 0; i < bodyNode.childNodes.length; i++) {
                           var node = bodyNode.childNodes[i];
                           //NOTE: This comparison does not handle the case where the XML namespace changes
                           if ("s:Fault" == node.nodeName) {
                           for (var j = 0; j < node.childNodes.length; j++) {
                               var faultStringNode = node.childNodes[j];
                               if ("faultstring" == faultStringNode.nodeName) {
                                   errorMessage = faultStringNode.text;
                                   break;
                               }
                           }
                           break;
                       }
                   }
               }
               catch (e) { };
            }
            return new Error(errorMessage);
         },
     __namespace: true
    };
    

    Here is source article - http://mileyja.blogspot.com/2011/06/execute-workflow-using-net-or-jscript.html


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Tuesday, May 15, 2012 10:02 AM
    Moderator
  • Hi,

    I have another code for you:

    function callworkfow(id) {
        try {
            var guid;
     
            if (id == null) {
                guid = Xrm.Page.data.entity.getId();
            }
            else
                guid = id;
     
            // assign workflow guid
            var WorkflowId = "89BA3166-948E-4B12-8A55-4BE01DEDAB0B";
     
            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\">" +
        Xrm.Page.context.getAuthenticationHeader() +
        "<soap:Body>" +
        "<execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "<request xsi:type=\"ExecuteWorkflowRequest\">" +
        "<entityid>" + guid + "</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;
     
            if (id == null)
                alert("Success");
            return (resultXml.xml);
        }
        catch (e) {
            alert("Failed to Execute");
        }
    } 

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 10:02 AM
  • Thanx for the quick reply.

    The workflow url is the following:

    http://..../sfa/workflow/edit.aspx?_CreateFromId=%7bFD140AAF-4DF4-11DD-BD17-0019B9312238%7d&_CreateFromType=7100&appSolutionId=%7bFD140AAF-4DF4-11DD-BD17-0019B9312238%7d&id=%7b133E4248-7E90-4DAC-81B3-83EFB1B24E6F%7d

    Which part is the workflowGuid?

    Thanx


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 10:10 AM
  • Hi,

    It is this one:

    id={133E4248-7E90-4DAC-81B3-83EFB1B24E6F}
    I hope this helps. If my responses answered your question, please mark these responses as an answer and also vote them as helpful.

    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 10:17 AM
  • What am I doing wrong? :-(

    function callworkflow(id) {
        try {
            var guid;
    
            if (id == null) {
                guid = Xrm.Page.data.entity.getId();
            }
            else
                guid = id;
    
            // assign workflow guid
            var WorkflowId = "133E4248-7E90-4DAC-81B3-83EFB1B24E6F";
    
            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\">" +
        Xrm.Page.context.getAuthenticationHeader() +
        "<soap:Body>" +
        "<execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "<request xsi:type=\"ExecuteWorkflowRequest\">" +
        "<entityid>" + guid + "</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;
          
            if (id == null)
                alert("Success");
            return (resultXml.xml);
        }
        catch (e) {
            alert("Failed to Execute");
        }
    } 
    
    
    
    
    
    function CallSalesupdateWorkflow()
    {
        callworkflow(Xrm.Page.data.entity.getId());
    }                                  


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 11:11 AM
  • Hi,

    What is the error you are getting?

    Also can you once verify your workflow GUID.

    If you are using CRM 2011 and just need the Process (Workflow) Guid, then you can go to Process from the Settings area and open the workflow. There is an option in the Main Bar, called Actions, select Copy Link.

    Then paste it in notepad, you should get something like this:

    Worklow Name
    <https://servername/sfa/workflow/edit.aspx?id=%7bXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX%7d>

    The workflow id will be: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 11:21 AM
  • Sales Process Stage Update 1
    <http://crmpro/ProkonCRM/sfa/workflow/edit.aspx?appSolutionId=%7bFD140AAF-4DF4-11DD-BD17-0019B9312238%7d&id=%7b133E4248-7E90-4DAC-81B3-83EFB1B24E6F%7d>

    No error.  The workflow just does not trigger.  I did try the first method you send, but it keeps sending me a login screen.


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 11:32 AM
  • Hi,

    I am assuming the Xrm.Page.data.entity.getId() is the id of the entity record on which this workflow will run. Also are you calling this CallSalesupdateWorkflow() function somewhere where it gets fired?

    Like call this function on load event or some event on the entity.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.



    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 11:40 AM
  • Hi Christo,

    Can you make sure that the CRM Asynchronous Service is running on the CRM Server?


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Tuesday, May 15, 2012 11:46 AM
  • Hi Yes to the first question. I am for now using the onload event to keep it simple. Also tried it on the onsave event but no result.

    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 11:48 AM
  • Yes it's running

    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 12:44 PM
  • Does this mean anything usefull?


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 12:46 PM
  • Hi Christo,

    Is your workflow available to run as On-Demand process? There is a checkbox in the workflow UI and you'll need to make sure it's checked.

    Otherwise, when you try to explicitly run the workflow via this method, it will throw up an error.

    Let me know how you go.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Tuesday, May 15, 2012 1:22 PM
  • Hi Dimaz 

    yes I did check it as on demand.


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 1:25 PM
  • Hi

    Many thanx to Ashish and Dimaz.  Should anyone ever come across this thread, here is the final code:

    function callworkflow() {
          
            var WorkflowId = "FD140AAF-4DF4-11DD-BD17-0019B9312238";
    
            /*Generate Soap Body.*/
            var soapBody = "<soap:Body>" +
                     "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
                     "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
                     "      <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
                     "      <WorkflowId>" + WorkflowId + "</WorkflowId>" +
                     "    </Request>" +
                     "  </Execute>" +
                     "</soap:Body>";
    
            /*Wrap the Soap Body in a soap:Envelope.*/
            var soapXml = "<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() +
                    soapBody +
                    "</soap:Envelope>";
    
            /* Create the XMLHTTP object for the execute method.*/
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
            xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    
            /* Send the XMLHTTP object. */
            xmlhttp.send(soapXml);
            
    }


    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Tuesday, May 15, 2012 2:23 PM
  • Hi Christo,

    I am glad I could help.

    Thanks for sharing the final code. Main thing is you workflow can now run from JScript.


    Ashish Mahajan, CRM Developer, CSG (Melbourne)
    My Personal Website: http://www.ashishmahajan.com
    My Blogs: http://ashishmahajancrm.blogspot.com.au and http://ashishmahajancrm.wordpress.com
    My Youtube Channel: http://www.youtube.com/user/ashishmahajanmscrm

    My Linkedin: View Ashish Mahajan's profile on LinkedIn
    My Twitter: https://twitter.com/#!/ashishmahajan74

    Tuesday, May 15, 2012 2:26 PM
  • Hello Christo and anyone else in this thread.

    I am basically doing the same code that is posted here however I am changing the GenerateAuthenticationHeader() function for a custom header that includes a systemuser which I want to use to run the workflow:

    var header="<soap:Header>" +
        "<CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "<AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
        "<CrmTicket xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\"></CrmTicket>"+
        "<OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">"+ORG_UNIQUE_NAME+"</OrganizationName>" +
        "<CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">"+ adminuserId +"</CallerId>" +
        "</CrmAuthenticationToken>" +
        "</soap:Header>"

    Where ORG_UNIQUE_NAME is the name of the organization where the workflow is located and the adminuserId is the systemuser is the user I want to run the workflow with and it has sys admin rights. I want to note that this code is running without a problem in the development environment it is only when I bring it to production when I receive the following error in the response:

     <?xml version="1.0" ?>

    - <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:Body>

    - <soap:Fault>

      <faultcode>soap:Server</faultcode>

      <faultstring>Server was unable to process request.</faultstring>

    - <detail>

    - <error>

      <code>0x80040204</code>

      <description>Invalid user auth.</description>

      <type>Platform</type>

      </error>

      </detail>

      </soap:Fault>

      </soap:Body>

      </soap:Envelope>

    This is the part that makes the call to execute the workflow:

    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);

    Do you have any idea of why works perfectly fine in the development environment and not in the production?

    Another note that I would like to add is that in the production environment IFD (Internet Facing Domain) is active. We have http on port 80 and https on port 443 for outside access.

    I've been trying to find out what the issue is for some days now and nothing.

    I would appreciate if you can share some ideas about what I could change in order for this to work.

    Thursday, July 26, 2012 4:02 PM