Starting a Dynamics CRM Custom Action from JavaScript

In a previous post, we demonstrated how to create a custom control that could look up all of the document templates for the Incident/Case entity. In this post, we will show you how to create a custom action that accepts the selected document template and Incident/Case, and apply the template against the entity. This work builds off our previous post, so I would suggest you read them first.

Creating the Custom Action

In order to create your custom action, open up the Processes section by going to Settings:Processes.

From here, we will create our new process. Click the New button. Fill out the form like you see below.

Now that you’ve created the action, we need to add an incoming process argument. This argument will be the document template that we select from our control. Below is how you can add the process argument. Make sure to click the + after you’ve set it up.

Next, we are going to set up the process. Scroll down to the bottom of the control to find the screen below. Click Add Step, then Perform Action.

Select the SetWordTemplate action. Then, click on the Set Properties button.

The gif below shows you how to properly set your arguments.

And that’s it! Your custom action is ready to be invoked via JavaScript.

Calling the Action

Now that we have created our custom action, it is time to invoke it from JavaScript. To do this, we will be using Dynamic CRM’s web API and an XMLHttpRequest. For readers of our previous post, this will be the body of our documentTemplateSelected method. For this method, we will need to know two things:

  1. The ID of the entity on which we are operating
  2. The ID of the document template we are working with

This method will return a Promise and execute the call asynchronously.

Note that on line 12, we are identifying the action by its name: new_CreateDocumentfromTemplate.

function triggerDocumentTemplateAction(entityId, selectedTemplateGuid)
{
  var data = {
    Template: {
      '@odata.type': 'Microsoft.Dynamics.CRM.documenttemplate',
      documenttemplateid: selectedTemplateGuid
    }
  };

  return new Promise((resolve, reject) => {
    var req = new XMLHttpRequest();
    req.open('POST', clientUrl + '/api/data/v9.0/incidents(' + entityId + ')/Microsoft.Dynamics.CRM.new_CreateDocumentfromTemplate');
    req.setRequestHeader('OData-MaxVersion', '4.0');
    req.setRequestHeader('OData-Version', '4.0');
    req.setRequestHeader('Accept', 'application/json');
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.setRequestHeader('Prefer', 'odata.include-annotations="OData.Community.Display.V1.FormattedValue"');
    req.onreadystatechange = function ()
    {
      if (this.readyState === 4)
      {
        req.onreadystatechange = null;
        if (this.status === 204)
        {
          resolve()
        } else
        {
          reject(this.statusText);
        }
      }
    };

    req.send(JSON.stringify(data));
  });
}

With this code, you’re ready to invoke the custom action! Wire this code up in the JavaScript we wrote in our previous post to start processing the document templates on the Case that you’ve opened.

Let us help you out!

Code Vanguard has built custom Dynamics 365 solutions for a variety of clients, including a global 500 corporation, supporting the U.S. Department of Justice, Department of Defense, and Department of the Treasury. . If you’d like Code Vanguard to help your organization with Dynamics development, feel free to reach out to us!


Check out some of our other posts about Dynamics CRM!

Tags:
James Stephens About the author

James is the founder of Code Vanguard and one of its developers. He is an applied mathematician turned computer programming. His focuses are on security, DevOps, automation, and Microsoft Azure.

No Comments

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.