Thursday, July 14, 2011

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.

If any issues just let me know.

Cheers!!

4 comments:

  1. Hello,
    Thanks for the great post.
    I'm trying to run the script you described but I get a login screen prompting me for my user and password and the workflow is not running.
    Any ideas?

    Thanks a million

    ReplyDelete
  2. The Same for me,It request username and password and when try to write mine or the admin password nothing happen.
    Thanks a lot

    ReplyDelete
  3. This piece of code a a joke.

    ReplyDelete
  4. This does not works with Chrome or Mozila, as
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    is supported only by IE.

    As MS CRM is now supported on multiple browsers. The above script will not run in Chrome/ Mozila.

    Do you have solution to call the workflow using odata?

    ReplyDelete