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.

Trackback

25 comments untill now

  1. Nice job!

  2. Thanks Michael. I owe you again. I couldn't get Steven's code to work so I just inserted the Workflow Process query into your tool and generated the code myself an it worked.

  3. This is an awesome post! I was looking for this exact functionality recently for a client. Thanks for sharing, Mitch!

    Michael Cross
    Navint
    http://www.navint.com

  4. Great information – thanks for putting it out there. This is EXACTLY what I was looking for and I was very happy to stumble across it. But…I can't get it to work. I get an error on the page when opening the Opportunity. I've gone over it again and again but it is beyond my (admittedly meager) ability to fix. Any chance you could point me in the right direction?

  5. abdullah @ 2007-12-31 05:54

    Hi
    When I paste your code in the load event of the entity I got the JS error Object Expected ? What I m missing?

  6. Hi,
    This is EXACTLY what I am looking to do as well… but I am getting a similar error as above when I copy in your scripts ("Object Expected"). I know just enough web service stuff to make me dangerous (but know javascript quite well), and I am stepping through the code… but if you have any other information or potential fixes from the above posts, any help would be greatly appreciated… thanks!!

    Mark

  7. Hi,
    This is exactly what I was looking for as well… but it looks like I am receiving the error message as the above posters (Object Expected) when I paste your code into the OnLoad. I am stepping through the code, but just wanted to see if you had any tips or fixes handy from the above posts. Thanks!!

    Mark

  8. Mitch: Great piece of code. To those commenters that are getting errors when trying to test this out, you need to re-type the code samples Mitch has posted instead of copying and pasting them. If you copy them you will get script errors due to the curly quotes and I think some escape characters are missing. I've posted the OnLoad script to my blog with a link so you can download it as a .txt file: http://icu-mscrm.blogspot.com/2008/03/start-workflow-from-javascript-onchange.html
    -Matt Wittemann
    CRM MVP

  9. Corné @ 2008-03-19 05:17

    Hi,
    has anybody managed to get this feature working at MSCRM 4.0?

  10. Hi,

    I use this code and get an error:

    soap:ClientServer was unable to process request.code>0×80040216
    An unexpected error occurred.
    Platform

    What is a solution ? Thanks!
    Paula

  11. As Matt mentioned, I think most of the errors everyone is experiencing are related to the Smart Quotes that the WordPress blog engine inserts. I've added additional code to correct the use of Smart Quotes so please try copying the code again and let me know what you see.

    Mitch

  12. Hello,

    i'm realtively new to ms crm 3 and have a problem-

    1. We are using ms CRM 3.0. They have sales process in place. Stage 5 is create an invoice-stage 6 is receive payment and stage 7 is delver product. The problem however is that sometimes the product is delivered before payment is done and this is not well represented on the workflow-I know that in CRM if you want workflow to skip a stage then it looses automation and then users have to manually move from one stage to the next. Is there a workaround this one?

    if there anything abvious i should do please forgive me

  13. Not really. That's pretty much the way the system is designed. CRM 4.0 may take care of some of these issues, but I haven't actually had time to review the Stages workflow in 4.0 at this point.

    Mitch

  14. Clotilde @ 2008-05-07 14:00

    Thank you so much. This is wonderfull Mitch !

  15. Thank you! it worked flawlessly

  16. firstly i dont seem to have any processes in the wfprocess table? instead i put in the GUID of the workflow i want to run, is that right?

    i am recieving the SOAP error

    Invalid format of input XML for request ExecuteWFProcess: required field 'EntityMoniker' is missing

    can anyone help, i am using crm 4

    many thanks

    Jonathan

  17. I used this solution instead, however thanks for the response

    Jonathan

  18. Vladislav Osmanov @ 2008-12-09 10:13

    Why don't use built in JS method in 4.0?

    Just:

    launchOnDemandWorkflowForm(", '3', '{86D760B4-9C30-4CB4-89CC-90D5A7195AB4}');

    where first param is never used, 2nd – ObjectTypeCode, 3rd – WF rule GUID…

  19. Vladislav,

    Using CRM's non-documented JavaScript methods is way unsupported – even though they work!

    Thanks for the tip.

    Mitch

  20. Hi Mitch,

    I hope you will still get this mail as this post is already pretty old and the rest of the world is on 4.0.. I work for a client who still runs CRM 3.0 and they would like to use data from the account form as input for an appointment created automatically by workflow. I have used your code to create the activity automatically by workflow and created custom fields to the account entity that need to go in the appointment. is there any way you can help me get that data through workflow in the appointment?

    Many thanks

    Aad

  21. Hi,
    I am calling a workflow from a Javascript code. After the execution of the workflow, i need to run a certain code which is based on the execution of the workflow. The problem is that the code depending on the workflow is running before the workflow ends execution. So how can i put a condition for the code to run after the workflow ends execution?
    Can i have the reply by email if possible?
    Thanks in advance

  22. Since there is absolutely no interaction between the workflow process and the CRM user interface, what you would like to do really can't be easily accomplished using JavaScript. You will have to have some sort of "trigger" that gets set or a workflow assembly that gets called at the end of the workflow itself.

  23. Hi Mitch,
    Thank you for your reply.
    Can you give me an idea of how i can accomplish what you said?
    Sabine

  24. Can't you use a crm workflow wait condition to achieve the same thing

Add your comment now

Sign up for our new KnowledgeBits service and get news, tips and tricks and more, delivered straight to your inbox.