Starting a dialog/workflow from a ribbon button

Now that we know how to hide a ribbon button and how to add our custom buttons to the forms, the next step is to make these buttons do something a little more intelligent than just bring up a pop-up message.

Getting ready

For the purpose of this recipe, we can either continue on the customizations started in the Adding a new ribbon button recipe at the beginning of this chapter, or re-create the button and the action as described there.

How to do it...

In order to kick off a workflow using a ribbon button, perform the following steps:

  1. Add your new form button as described in the first recipe of this chapter.
  2. Create your custom workflow as part of the same solution.

    Note

    We will require the workflow ID in our function. For the sake of simplicity, I will show you how to find out this ID manually and hard code it into your JavaScript function.

  3. Start the workflow manually.
  4. Go to System Jobs and find the job running the workflow.
  5. Do a copy link. Paste that link into Notepad. You will see the following at the end of the link:
    id=%7b[GUID]%7d

    Where [GUID] is of the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.

  6. Copy that GUID, as we will need it as a parameter in our JScript function.
  7. Add a new JScript web resource that contains our function.
  8. Add the following JScript function to our web resource:
    function launchWorkflow(dialogID, typeName, recordId)
    {
    // Load modal
    var serverUri = Xrm.Page.context.getServerUrl() + "/cs/dialog/rundialog.aspx";
     window.showModalDialog(serverUri + '?DialogId=' + dialogID + '&EntityName=' + typeName +
    '&ObjectId=' + recordId, null, 'width=615,height=480,resizable=1,status=1,scrollbars=1'),
    // Reload form
     window.location.reload(true);
    }
  9. Export your solution to a location on your machine.
  10. Unzip the solution file and remember the location.
  11. Edit the customizations.xml file within the unzipped solution.
  12. Find the Lead entity section.
  13. Find the RibbonDiffXml section within the Lead tag.
  14. Find the ribbon button definition, and within it find the Actions segment.
  15. Replace the Actions with the following XML code:
    <Actions>
     <JavaScriptFunction Library="$webresource:Nav_JS_Common_Lib" FunctionName="launchWorkflow">
    <!– workflowId, entityName, entityId –>
     <StringParameter Value="{e1037cac-756c-46e7-96d0-cdc572eafc65}" />
     <StringParameter Value="lead" />
     <CrmParameter Value="FirstPrimaryItemId" />
     </JavaScriptFunction>
    </Actions>
  16. Save and Publish your solution.
  17. Test it by creating a new Lead and clicking the new ribbon button.

How it works...

The solution presented is comprised of the following main actions:

  1. Create a system workflow.
  2. Add a new ribbon button.
  3. Define the ribbon button action through JScript to point it to the custom workflow created.

Within the customizations.xml file, the following tags are important:

  • CustomActions: It allows us to define the location of where the ribbon button appears.
  • CommandUIDefinition: It is a container control that defines user interface elements along with properties such as label, tooltip, and icon.
  • CommandDefinition: It defines the command to be executed.
  • Actions: It allows us to customize the JScript function to execute or the URL to forward to. In this section we can also define parameters.
  • CrmParameter: It permits us to pass information directly from the calling form. In our example we passed the GUID of the Lead record.

In order to process a workflow rather than a dialogue, we can use a similar approach, and point to the workflow ID. In addition, for more complex situations, we can launch workflows from a dialogue, or vice versa.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.145.217