Home > MS CRM 4.0 > Triggering a Workflow with JavaScript in CRM 4.0

Triggering a Workflow with JavaScript in CRM 4.0

Ref: http://blogs.inetium.com/blogs/microsoftcrm/archive/2008/10/29/triggering-a-workflow-with-javascript-in-crm-4-0.aspx

Even though the workflow engine in CRM 4.0 supports a ton of new ways to trigger the exection of a workflow there may be cases where a button on the Form is more useful.  The following example shows how you can provide an button that allows user’s to escalate a case to

When a new case is created the case is assigned to queue monitored by Tier 1 support.  As they work they case they may need to escalate to the Tier 2 support team.  As part of the escalation process the case is routed to a Tier 2 Support Queue in CRM and an e-mail is sent out to the corresponding team informing them a case had been escalated.

Option 1:  Create a Workflow and use the Run Workflow Button
You could create a workflow that would have 2 steps.  The first step is to assign the case to the Tier 2 Support Queue.  The second step would create an e-mail and send it to the Tier 2 Distribution Group.  Tier 1 support representatives could then use the Run Workflow button inside of the case to trigger this action.  While this would accomplish the task at hand, if there are a lot of workflows tied to cases it may be cumbersome to scroll the list of workflows in order to choose the right one.  It would also take Tier 1 support a minimum of 3 clicks to escalate this case.

Option 2:  Add A Button on the Case Form To Escalate the Case
The same scenario in Option 1 still applies, however this time the entire process can be automated by just 1 click of a button.

Step 1:  Create the Tier 2 Support Queue in CRM

  1. In CRM click on Settings in the Wunderbar
  2. Click Business Managment and then select Queues
  3. Click New to create a New Queue
  4. Name the Queue Tier 2 Support
  5. Click Save & Close to create the Queue

Step 2:  Create the necessary workflow rule
Our workflow rule will have 2 steps, one to assign the case to the Tier 2 Support Queue and a second step to send the e-mail.

  1. In CRM Navigate to the Settings section of CRM
  2. Click Workflows
  3. Click New Workflow
  4. Give it a name like:  Tier 2 Support Escalation
  5. In the Entity drop-down box choose Case and click Ok
  6. Set the workflow to be On-Demand and clear the other triggering events (Create/Update/Status Change etc)
  7. Build your workflow logic using the editor
  8. Save the workflow
  9. Publish the workflow

Step 3:  Find the GUID of the Workflow

  1. Press Ctrl + N, to open the Workflow in a new IE Window. 
  2. Look in the URL and find the ID of the workflow, copy the GUID (do not copy the { or }
  3. Close both workflow windows.

Step 4:  Update the ISV.Config to add the button to the Case Form

  <Button JavaScript="
var _return = window.confirm(‘Are you sure you want to escalate this case to Tier 2 Support?’);
var caseid = crmForm.ObjectId;
var authenticationHeader = GenerateAuthenticationHeader();
//Prepare the SOAP message.
var xml = &quot;&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;&quot;+
&quot;&lt;soap:Envelope xmlns:soap=&apos;http://schemas.xmlsoap.org/soap/envelope/&apos;&quot;+
&quot; xmlns:xsi=&apos;http://www.w3.org/2001/XMLSchema-instance&apos;&quot;+
&quot; xmlns:xsd=&apos;http://www.w3.org/2001/XMLSchema&apos;&gt;&quot;+
&quot;&lt;Execute xmlns=&apos;http://schemas.microsoft.com/crm/2007/WebServices&apos;&gt;&quot; +
&quot;&lt;Request xsi:type=&apos;ExecuteWorkflowRequest&apos;&gt;&quot; +
&quot;&lt;EntityId&gt;&quot; + caseid + &quot;&lt;/EntityId&gt;&quot; +
&quot;&lt;WorkflowId&gt;F52FD830-1E30-4E9C-B8D6-F05CEB65EEDE&lt;/WorkflowId&gt;&quot; +
&quot;&lt;/Request&gt;&quot; +
&quot;&lt;/Execute&gt;&quot; +
&quot;&lt;/soap:Body&gt;&quot; +
var xHReq = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
xHReq.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
xHReq.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
xHReq.setRequestHeader(&quot;Content-Length&quot;, xml.length);
window.alert(‘The Case Has Been Escalated To Tier 2 Support’);
" ValidForCreate="0" ValidForUpdate="1">
    <Title LCID="1033" Text="Escalate the Case To Tier 2 Support"/>

Step 5:  Import the Updated ISV.Config File

Now you should be able to open up an existing case.  In the Toolbar you will see the Custom Button.  When you click the button you will get prompted as to whether or not you really want to escalate the case.  Once you click Ok the workflow will be triggered via the SOAP call in the button code.  When the SOAP call has executed the last prompt will indicate that the workflow was triggered.  If you check the workflow section of the case you should see that the workflow has now fired.

This works great not only for Cases but for any entity where you may want an easier way to trigger an On-Demand workflow.

Categories: MS CRM 4.0
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: