One of the Microsoft Partners I work with asked if you could launch a Workflow rule when a CRM user changed a specific field on the Opportunity form.  That topic sounded familiar and after digging around a bit, I found a newsgroup post where Steven G had provided just such a solution.  I've reworked Steven's code a bit to make it more generic and here is what you need to do to make this solution work:

 

Step 1: Locate the Workflow Process ID

You will need the ID of the CRM Workflow Process that you will be running.  Open SQL Query Analyzer or SQL Server Management Studio and run the following script against the MSCRM database.

Note: Replace "my workflow rule" with the name of the rule you are searching for.

   1: select 
   2:     ProcessID,
   3:     [name]
   4: from 
   5:     wfprocess 
   6: where 
   7:     [name] like 'my workflow rule%' 
   8:     and 
   9:     ProcessTypeCode = 1
  10:  

Copy the ProcessID value that is returned by the script.

 

Step 2: Add code the the  OnLoad Event of the CRM Entity you will be using

The following code needs to be added to the OnLoad Event of the Form.

   1: StartWorkflow = function(entityName, entityId, workFlowProcessID)
   2: {
   3: var xml = "" + 
   4: "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
   5: "<soap:Envelope xmlns:soap=\"" +
   6: "http://schemas.xmlsoap.org/soap/envelope/\" " +
   7: " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
   8: "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 
   9: "<soap:Body>" + 
  10: "<Request xsi:type=\"ExecuteWFProcessRequest\" "+
  11: "xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" + 
  12: "<ProcessId>" +
  13: workFlowProcessID +
  14: "</ProcessId>" + 
  15: "<EntityMoniker>" + 
  16: "<Id xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">" +
  17: entityId +
  18: "</Id>" + 
  19: "<Name xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">" +
  20: entityName +
  21: "</Name>" + 
  22: "</EntityMoniker>" + 
  23: "</Request>" + 
  24: "</soap:Body>" + 
  25: "</soap:Envelope>" + 
  26: "";
  27:  
  28: var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  29:  
  30: xmlHttpRequest.Open("POST", 
  31:   "/mscrmservices/2006/CrmService.asmx", 
  32:   false);
  33:  
  34: xmlHttpRequest.setRequestHeader("SOAPAction",
  35:   "http://schemas.microsoft.com/crm/2006/WebServices/Execute");
  36:  
  37: xmlHttpRequest.setRequestHeader("Content-Type", 
  38: "text/xml; charset=utf-8");
  39:  
  40: xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  41: xmlHttpRequest.send(xml);
  42:  
  43: var resultXml = xmlHttpRequest.responseXML;
  44:  return resultXml.xml;
  45: }

 

Step 3: Add code to the OnChange Event of the Form Attribute

This code goes in the OnChange Event for the Form Attribute we're checking. 

Since we must have an ID assigned to the record, this code will only work if you are Updating the record.  You may also wish to add additional logic to check various conditions before running the Workflow Rule.

Note: Replace the ID assigned to workflowProcessID with the value you found in step 1.

   1: if (crmForm.FormType == 2)
   2: {
   3: var workflowProcessID = '3B6CFF1A-A117-4243-8134-8DA612CC3A5C'; 
   4: var entityName = crmForm.ObjectTypeName; 
   5: var entityID = crmForm.ObjectId;
   6:  
   7: var retVal = '';
   8:  
   9: retVal = StartWorkflow(entityName, entityID, workflowProcessID);
  10:  
  11: //alert(retVal);
  12: }

 

That's pretty much it.  I would advise that you remove the comment in line 11 so that you can see the XML that is returned by the CRM web service so that you can ensure that the code is working properly.

 

Additional Notes:

Michael Höhne has a tool that allows you to capture the output of a CRM web service call and, among other things, convert that information to JavaScript.  This tool was invaluable while I was recreating Steven's work and debugging my function.