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.
18 Responses
Michael Hoehne
15|Jun|2007 1Nice job!
mitch
15|Jun|2007 2Thanks 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.
mcross9095
29|Jun|2007 3This 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
Neta B
16|Oct|2007 4Great 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?
abdullah
31|Dec|2007 5Hi
When I paste your code in the load event of the entity I got the JS error Object Expected ? What I m missing?
mrosmus
25|Feb|2008 6Hi,
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
mrosmus
25|Feb|2008 7Hi,
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
mwittemann
06|Mar|2008 8Mitch: 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
Corné
19|Mar|2008 9Hi,
has anybody managed to get this feature working at MSCRM 4.0?
paulach
27|Mar|2008 10Hi,
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
mitch
02|Apr|2008 11As 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
puleng
11|Apr|2008 12Hello,
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
mitch
11|Apr|2008 13Not 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
Clotilde
07|May|2008 14Thank you so much. This is wonderfull Mitch !
Matt
31|Jul|2008 15Thank you! it worked flawlessly
Jonathan Nachman
13|Aug|2008 16firstly 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
mitch
13|Aug|2008 17Jonathan,
Take a look at Jim's solution:
http://jianwang.blogspot.com/2008/07/crm-40-use-javascript-excutecalllaunch.html
Mitch
Jonathan Nachman
21|Aug|2008 18I used this solution instead, however thanks for the response
Jonathan
Leave a reply
Search
Categories
Archives
Meta
Most Viewed
Tags
A design creation of Design Disease
Copyright © 2007 - Mitch Milam's Microsoft Discussions - is proudly powered by WordPress
InSense 1.0 Theme by Design Disease brought to you by HostGator Web Hosting.