Tuesday 10 April 2012

Triggering a Workflow from a Ribbon Button


Add Button to Entity Ribbon

Export the entity in question and open up customizations.xml. Add the following within the CustomActions tag…

01<CustomAction
02  Id="CA_MyFirstButton"
03  Location="Mscrm.Form.account.MainTab.Save.Controls._children"
04  Sequence="31">
05  <CommandUIDefinition>
06    <Button
07      Id="B_MyFirstButton"
08      Command="Mscrm.RunSomeJS"
09      LabelText="Button Label"
10      ToolTipTitle="Run Test Workflow"
11      ToolTipDescription="Runs the test workflow to see if this works"
12      TemplateAlias="o1"
13      Image16by16="/_imgs/ribbon/tools_16.png"
14      Image32by32="/_imgs/ribbon/tools_32.png" />
15  </CommandUIDefinition>
16</CustomAction>

Location sets which area of the ribbon the button will appear on.
Sequence sets where within that ribbon it will appear.
LabelText sets the text which appears under the icon on the ribbon.
TemplateAlias (who knows!)

Create the click action

Now we need to add the command Mscrm.RunSomeJS within the CommandDefinitions section.

01<CommandDefinition
02  Id="Mscrm.RunSomeJS">
03  <EnableRules>
04    <EnableRule Id="Mscrm.Enabled" />
05  </EnableRules>
06  <DisplayRules>
07    <DisplayRule Id="Mscrm.CanWriteAccount" />
08  </DisplayRules>
09  <Actions>
10    <JavaScriptFunction
11      FunctionName="TriggerWorkflow"
12      Library="$Webresource:isah_test">
13      <StringParameter
14        Value="{guid of account entity}" />
15      <StringParameter
16        Value="{7D78531A-23EB-4042-9626-1D80DFC10A8D}" />
17    </JavaScriptFunction>
18  </Actions>
19</CommandDefinition>

EnableRules sets if the button is greyed out on the toolbar (always enabled in this example)
DisplayRules sets if the button appears on the toolbar (if the user can write to the account entity in this example)
FunctionName sets the name of the function to run within the web resource
Library sets the name of the web resource JavaScript library to access
StringParameters
in this case are the guid of the account entity (not used currently) and the guid of the workflow to trigger

JavaScript to Fire the Workflow


01function TriggerWorkflow(entityName,workflowGuid) {
02 
03  var xx = prompt(Xrm.Page.data.entity.getId(),Xrm.Page.data.entity.getId());
04 
05  /*Generate Soap Body.*/
06  var soapBody = "<soap:Body>" +
07                 "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
08                 "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
09                 "      <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
10                 "      <WorkflowId>" + workflowGuid + "</WorkflowId>" +
11                 "    </Request>" +
12                 "  </Execute>" +
13                 "</soap:Body>";
14 
15  /*Wrap the Soap Body in a soap:Envelope.*/
16  var soapXml = "<soap:Envelope " +
17                "  xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' " +
18                "  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
19                "  xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
20                GenerateAuthenticationHeader() +
21                soapBody +
22                "</soap:Envelope>";
23 
24  /* Create the XMLHTTP object for the execute method.*/
25  var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
26  xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
27  xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
28  xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
29 
30  /* Send the XMLHTTP object. */
31  xmlhttp.send(soapXml);
32 
33}

No comments:

Post a Comment