Firing workflows against multiple records from a ribbon button


UPDATE (2013-04-25):

I recently ran into a problem with this code. When running against large numbers of records (200+) the URL can become very long. This can lead to a number of issues…

1) IIS can hit configured URL length limits causing 500 errors to appear within CRM dialogs. These can be changed in IIS Manager – Request Filtering, Edit Feature Settings.

iis_config3_zoom75

2) I use IE9. Even though the popup blocker was disabled, a dialog would appear saying “A Microsoft Dynamics CRM window was unable to open, and may have been blocked by a pop-up blocker…”. To resolve this, I removed line 21 in my launchWorkflowMulti function. Turns out you don’t have to specify the list of IDs twice (or you no longer have to in current rollups).

Screenshot---25_04_2013-,-18_06_09


I wanted to be able to allow users to “authorise” a set of records from the homepage grid. In this case, authorising a record simply meant executing a particular workflow against it.

The functionality needed to be similar to the standard “Run Workflow” button – with a modal progress bar but without having to select which workflow to run first. Critically the user needed to be able to select any number of records to be authorised in one operation.

Here’s how I got it working…

Configure Ribbon Buttons

Configure your ribbon in the usual way. Or you can give this a try – CRM 2011 Visual Ribbon Editor (http://crmvisualribbonedit.codeplex.com/).

<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls.Approve" Location="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls._children" Sequence="99">
      <CommandUIDefinition>
        <Button Id="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls.Approve.Button" Command="Mscrm.HomepageGrid.new_casesummary.Approve.Command" LabelText="Approve Summary" ToolTipTitle="Approve Case Summary" ToolTipDescription="Run approval procedure for the current Case Summary" TemplateAlias="o1" Image16by16="$webresource:new_icon_thumbsup_16" Image32by32="$webresource:new_icon_thumbsup_32" />
      </CommandUIDefinition>
    </CustomAction>
    <CustomAction Id="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls.Authorise" Location="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls._children" Sequence="100">
      <CommandUIDefinition>
        <Button Id="Mscrm.HomepageGrid.new_casesummary.MainTab.Workflow.Controls.Authorise.Button" Command="Mscrm.HomepageGrid.new_casesummary.Authorise.Command" LabelText="Authorise Summary" ToolTipTitle="Authorise Case Summary" ToolTipDescription="Run authorisation procedure for the current Case Summary" TemplateAlias="o1" Image16by16="$webresource:new_icon_thumbsup_16" Image32by32="$webresource:new_icon_thumbsup_32" />
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <CommandDefinitions>
    <CommandDefinition Id="Mscrm.HomepageGrid.new_casesummary.Approve.Command">
      <EnableRules></EnableRules>
      <DisplayRules></DisplayRules>
      <Actions>
        <JavaScriptFunction Library="$webresource:new_script_ribbonHandler" FunctionName="launchWorkflowMulti">
          <StringParameter Value="Are you sure you want to approve the selected records?" />
          <StringParameter Value="E4D7011F-A05F-4676-8630-56D93D89B2C7" />
          <CrmParameter Value="SelectedControlSelectedItemIds" />
          <BoolParameter Value="true" />
          <CrmParameter Value="SelectedEntityTypeCode" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
</RibbonDiffXml>

So, when clicked, the button fires the function launchWorkflowMulti method within my new_script_ribbonHandler script.

The function’s parameters are:-

  • Confirmation Message – Message to prompt() the user with before continuing
  • GUID – The GUID of the workflow we want to run
  • SelectedControlSelectedItemIds – CRM substitutes this parameter with the list of currently selected records from the grid
  • True/False – This boolean parameter dictates if the screen will be refreshed after the procedure is complete.
  • SelectedEntityTypeCode – CRM substitutes this parameter with the Object Type Code for the current entity

For some reason you have to provide the list of GUIDs both as part of the URL and as an array to the arguments parameter of the built-in openStdDlg() function.

Finally, here’s the function itself:-

function launchWorkflowMulti(confirmMessage, workflowId, recordIds, reload, objectTypeCode) {
  
  // Check variables
  if (objectTypeCode == "") {return;}
  if (recordIds == "") {return;}
  
  // Show confirmation to user
  if (confirmMessage != "") {
    if (!confirm(confirmMessage)) {return;}
  }
  
  // Split recordID list into array (on comma)
  var aRecords = (recordIds + "").split(",");
  if (aRecords.length == 0) {return;}
  
  // Open dialog
  var url = prependOrgName("/_grid/cmds/dlg_runworkflow.aspx")
    + "?iObjType=" + CrmEncodeDecode.CrmUrlEncode(objectTypeCode)
    + "&amp;iTotal=" + CrmEncodeDecode.CrmUrlEncode(aRecords.length)
    + "&amp;wfId=" + CrmEncodeDecode.CrmUrlEncode("{" + workflowId + "}") + ""
    + "&amp;sIds=" + CrmEncodeDecode.CrmUrlEncode(recordIds);
  
  var oresult = openStdDlg(url, aRecords, 500, 200);
  
  if (reload) {window.location.reload(true);}
  
}
Advertisements
Leave a comment

17 Comments

  1. Daxaar

     /  May 24, 2012

    I’ve been using the viz ribbon editor against on-premise for a while without issue. Have you experienced otherwise?

    Reply
    • Yeah sorry I didn’t mean on-premise. We actually host with a cloud-based third party who use an ADFS authentication system. A lot of the little CRM tools on codeplex can’t get around it.

      Reply
  2. Hamed

     /  November 4, 2012

    Hi !
    thanks for the post
    I’ve done the things in your post in the exact order
    but when I click the ribbon button after the Confirmation box, i get an error box witch says “An Error has occurred)
    i seems that the the error is coming right after calling the “openStdDlg” function:
    var oresult = openStdDlg(url, aRecords, 500, 200);

    I’ve g CRM 2001 on-premise with R8 installed

    so what do you think is the problem?

    Reply
  3. Chuck

     /  November 15, 2012

    Just figured out why this wasn’t working for me! In the code for the variable url, you don’t need the html code for the ampersand symbol because it’s enclosed in quotes.

    Simply put ‘&iTotal=’. Do the same for wfId and sIds. It’s that easy! Well, I say this now but it took me a while to figure out.

    Reply
  4. Chuck

     /  November 15, 2012

    Oops, meant to type “&iTotal=” with double quotes.

    Reply
  5. Michael Trenfield

     /  January 17, 2013

    Im confused as to where the workflow id is gathered from, do you insert it where is says workflow id?
    &wfId=” + CrmEncodeDecode.CrmUrlEncode(“{” + workflowId + “}”) + “”

    Reply
    • The workflow ID is passed from the ribbon XML. In the XML at the top of the post it’s on line 21.

      Reply
    • Michael Trenfield

       /  January 17, 2013

      Ignore that, i assume it replaces the ID you provided in string paramater. I’m getting an alert box popping up now saying undefined?

      Reply
  6. Michael Trenfield

     /  January 17, 2013

    Thanks for the reply Pete, sorry I didn’t notice that until after I had posted again. Any idea why it would come up as undefined?

    Reply
  7. It just comes up with an alert box saying ‘Undefined’ and nothing else? Weird.

    Is the workflow you are trying to run published? Are you running this from within a HomePageGrid ribbon section – if not, you’ll have to change both of the “CRMParameter” values in the XML from “Selected…” to “Primary…” (http://msdn.microsoft.com/en-us/library/gg309332.aspx).

    Other than that I can only suggest you try removing parts of the javascript in-turn to figure out what line is generating the error and work back from there.

    Based on the comments, I need to have another look at this post and make a few fixes. Sadly got no time at the moment. Sorry can’t be of more help right now.

    Reply
  8. Michael Trenfield

     /  January 17, 2013

    Thanks Pete, The workflow is published and the button is being run from the Activity’s home page grid ribbon. The message is undefined with the options of ok or cancel, clicking cancel nothing else happens. clicking ok I get a crm error box pop up.

    Reply
  9. Sounds like the javascript isn’t receiving any of the parameters from the XML. Could I see the RibbonDiff XML you are using ?

    Put it on pastebin (http://pastebin.com/) and reply with the link.

    Reply
  10. Michael Trenfield

     /  January 17, 2013

    Hmm it seems that manually editing the xml in the ribbon workbench isnt including the code you provided.

    I’ll export the solution and manually add it in and see if it works.

    Reply
  11. Michael Trenfield

     /  January 17, 2013

    Thanks for your help Pete, the issue was with the XML amendments not being included. All works great now.

    Reply
  12. Excellent. Glad you got it working.

    Reply
  13. Bill Benjamin

     /  March 1, 2013

    I was having an issue where this was breaking if I tried to execute the workflow against more than 43 records at once. The issue was due to the length of the URL that is called to open the dialog. The “sIds” parameter is not needed on the URL, since you are passing the records as a parameter to openStdDlg. Removing that from the URL corrected this issue.

    Reply

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: