Answered by:
Trigger Workflow from JavaScript

Question
-
Hi all,
I have a custom entity where i place my custom button on the HomePage Grid and on a click of that button i want a workflow to be trigerred for the selected record using JavaScript.
Went through most of the blogs but i dint find the proper solution, can anyone please post the code to do that only for selected records?
Thanks in Advance
Friday, October 19, 2012 2:02 PM
Answers
-
Hi,
Here is a post showing you how to do this without writing a single line of code!
http://www.develop1.net/public/post/No-Code-Workflow-Shortcut-Ribbon-Button.aspx
hth.
Scott Durow
Read my blog: www.develop1.net/publicFollow Me on Twitter
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Proposed as answer by Scott Durow (MVP)MVP, Editor Friday, October 26, 2012 2:13 PM
- Marked as answer by Nandan21 Saturday, October 27, 2012 5:24 AM
Friday, October 26, 2012 2:13 PMAnswerer
All replies
-
Hi,
check out this post.
You can create a ribbon button for this purpose. In the Ribbon Button you can call a JS function and pass the selected records' Ids as parameter.
You can use the ExecuteWorkflowRequest. Check out this post for an implementation in javascript.
Greetings,
Pavlos
Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
View my latest gallery contribution here.
Visit my blog here.
- Edited by Pavlos Panagiotidis Friday, October 19, 2012 2:18 PM
Friday, October 19, 2012 2:08 PM -
Hi:
We can call both workflow (or) dialogue in a ribbon button ,
1. create a button in home page of your entity using xml (or) visual ribbon editor tool ,
by using visual ribbon editor tool we can easily create a button on home page , form , sub grid.
Refer bellow Blog
http://crmvisualribbonedit.codeplex.com/
http://garethtuckercrm.com/2011/01/18/how-to-view-ribbon-definitions-in-crm-2011-3/
2. using java script call that work flow , by just adding Actions rule in that ribbon button.
refer bellow blog to call your java script in ribbon button
http://garethtuckercrm.com/2011/01/18/how-to-view-ribbon-definitions-in-crm-2011-3/
3. Java Script to call workflow
refer bellow link
http://petecrm2011.wordpress.com/2011/01/21/ribbonworkflow/
http://social.microsoft.com/Forums/en/crm/thread/5eb92b24-e21b-4fc7-a0d9-f82d7acabacc
http://community.dynamics.com/product/crm/f/117/p/77417/143094.aspx
http://guruprasadcrm.blogspot.in/2011/02/from-ribbon-button-calling-on-demand.html
Sunday, October 21, 2012 10:19 AM -
Hi Nandan,
I created one custom button on Account Home page grid and Customize the ribbon to run the Workflow on selected Account record.I tested it and its working fine.
Below is the RibbonDiff and webresoure details.
Note:
1.Add a webresouce named "new_commomRibbonJS" and add "TriggerWorkflow" method as given below.
2.Pass the "WorkflowId" in the "JavaScriptFunction" in RibbobDiff like
<StringParameter Value="0C5C240B-7D7A-434A-AC9B-805919B170FC" />
3.Workflow will execute for the first selected Account on Grid (Make Sure to select one record at a time)
function TriggerWorkflow( EntityId , WorkflowId ) { 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>" + EntityId[0] + "</EntityId>" + " <WorkflowId>" + WorkflowId + "</WorkflowId>" + " </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; return (resultXml.xml); }
<RibbonDiffXml> <CustomActions> <CustomAction Id="CA_MyFirstButton" Location="Mscrm.HomepageGrid.account.MainTab.Workflow.Controls._children" Sequence="31"> <CommandUIDefinition> <Button Id="B_MyFirstButton" Command="Mscrm.RunSomeJS" LabelText="Button Label" ToolTipTitle="Run Test Workflow" ToolTipDescription="Runs the test workflow to see if this works" TemplateAlias="o1" Image16by16="$Webresource:new_star32" Image32by32="$Webresource:new_star32" /> </CommandUIDefinition> </CustomAction> </CustomActions> <Templates> <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates> </Templates> <CommandDefinitions> <CommandDefinition Id="Mscrm.RunSomeJS"> <EnableRules> <EnableRule Id="Mscrm.Enabled" /> </EnableRules> <DisplayRules> <DisplayRule Id="Mscrm.CanWriteAccount" /> </DisplayRules> <Actions> <JavaScriptFunction FunctionName="TriggerWorkflow" Library="$Webresource:new_commomRibbonJS"> <CrmParameter Value="SelectedControlSelectedItemIds" /> <StringParameter Value="0C5C240B-7D7A-434A-AC9B-805919B170FC" /> </JavaScriptFunction> </Actions> </CommandDefinition> </CommandDefinitions> <RuleDefinitions> <TabDisplayRules /> <DisplayRules /> <EnableRules /> </RuleDefinitions> <LocLabels /> </RibbonDiffXml>
Hope this helps,
Yusuf
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
- Edited by Mohammad Yusuf Ansari Monday, October 22, 2012 7:52 AM
- Proposed as answer by Mohammad Yusuf Ansari Monday, October 22, 2012 7:53 AM
Monday, October 22, 2012 7:51 AM -
Hi Yusuf,
Thanks for ur reply but i wanted to trigger workflow for multiple selected records.
Friday, October 26, 2012 7:18 AM -
Hi Nandan,
Modify the "TriggerWorkflow" function and "<Actions>" in Ribbondiff as given below
Update <Actions> with this
<Actions> <JavaScriptFunction FunctionName="TriggerWorkflow" Library="$Webresource:new_commomRibbonJS"> <CrmParameter Value="SelectedControlSelectedItemCount" /> <CrmParameter Value="SelectedControlSelectedItemIds" /> <StringParameter Value="0C5C240B-7D7A-434A-AC9B-805919B170FC" /> </JavaScriptFunction> </Actions>
Update "TriggerWorkflow" function with this
function TriggerWorkflow(selectedEntityCount, EntityId , WorkflowId ) { for( int i=0;i< selectedEntityCount;i++) { 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>" + EntityId[i] + "</EntityId>" + " <WorkflowId>" + WorkflowId + "</WorkflowId>" + " </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; return (resultXml.xml); } }
I believe it should work.
Regards,
Yusuf
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
- Edited by Mohammad Yusuf Ansari Friday, October 26, 2012 10:05 AM
- Proposed as answer by Pavlos Panagiotidis Friday, October 26, 2012 10:11 AM
Friday, October 26, 2012 10:05 AM -
Hi,
Here is a post showing you how to do this without writing a single line of code!
http://www.develop1.net/public/post/No-Code-Workflow-Shortcut-Ribbon-Button.aspx
hth.
Scott Durow
Read my blog: www.develop1.net/publicFollow Me on Twitter
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Proposed as answer by Scott Durow (MVP)MVP, Editor Friday, October 26, 2012 2:13 PM
- Marked as answer by Nandan21 Saturday, October 27, 2012 5:24 AM
Friday, October 26, 2012 2:13 PMAnswerer -
Hey Scott
This post is really helpful which resolved my problem
Thanks a lot
Saturday, October 27, 2012 5:24 AM -
Hi Scott
Please if u can help me out with this issue which i have posted
http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/6ccf8b37-daf9-4ac5-83a4-9d14c7fef2ab
Saturday, October 27, 2012 5:25 AM