Microsoft Dynamics 365, MSCRM

Auto-generate word document in Dynamics 365 CRM using Document Templates, Workflow and Javascript

Whether it is a Word document or an Excel sheet, documents are always required in a Business Application. I came across a requirement that needed a Word document to be auto-generated in Microsoft Dynamics 365 CRM.

Requirement:

On click of a button in the Contact form, generate a Word document that consists of all the details of the Contact like name, date of birth, mobile number, email address.

Solution:

I have divided the solution into 4 main parts to explain it easily.

  1. Creating a trigger field and a javascript web resource which will update the field.
  2. Create a button on the form ribbon using Ribbon Workbench which on click will run the javascript function to update the trigger field.
  3. Create a word template and map all the required values to it in Microsoft Word and then upload the template back to CRM.
  4. Create a Workflow that will run when the trigger field is updated and generate a Word document.

Let’s begin!

Creating a trigger field and a javascript web resource which will update the field.

Go to Settings -> Customizations -> Customize the System – under Components -> Entities -> Contact -> select Fields and create a new field (with Name = new_DocumentTrigger, Data Type = Two Options and Field Type = Simple), save it and publish the customization.

In the Solutions Explorer, create a new Web Resource of type Script (JScript) and name it as new_DocumentTrigger. Upload the JS file with the source code as shown below and publish the customization.

Create a web resouce

function TriggerDocument()  {    
  var triggerValue = true;    
  triggerValue = Xrm.Page.getAttribute("new_documenttrigger").getValue();    
  if (triggerValue == true)    {        
    Xrm.Page.getAttribute("new_documenttrigger").setValue(false);        
    Xrm.Page.data.entity.save();    
  }    
  else    {        
    Xrm.Page.getAttribute("new_documenttrigger").setValue(true);        
    Xrm.Page.data.entity.save();    
  }
}

Create a button on the form ribbon using Ribbon Workbench which on click will run the javascript function to update the trigger field.

We need to create a command which will trigger the javascript function and then assign that command to a button on the form ribbon.

Go to Ribbon Workbench -> select Solution -> select Contact entity -> click on ‘+’ button besides the Commands tab and add a new command.

Give the command Id as “new.contact.Command0.triggerDocument” and click on Add Action and select Javascript Action.

In the Custom Javascript Action, select Library as “new_DocumentTrigger” from the web resource and Function Name as “TriggerDocument”.

Next, Go to the Buttons tab and click on ‘+’ button to add a new button. Give the button Id as “new.contact.Button2.TriggerButton”, select the previously created command from the dropdown and give Label, Alt, Tool Tip Title and Description as “Generate Contact Document”.

Ribbon-Create-Command-Step-1
Step 1
Ribbon Create Command - Step 3
Step 2
Ribbon Create Command - Step 3
Step 3

Drag the newly created button and place it on the form ribbon and publish the customization.

Create a word template and map all the required values to it in Microsoft Word and then upload the template back to CRM.

We need to create a word document template and download it so that we can map all the fields using Microsoft Word.

Go to Settings -> Templates -> Document Templates -> click on “New”.

A pop up will appear and will ask for template type and data, select “Word Template” and filter data by the entity and select Contact entity. Next, click on the “Select Entity” button and select all the relationships and click on “Download Template” to download the template.

Create Doc Template - Step 1
Create Doc Template - Step 2

Once downloaded, open the template in Microsoft Word, Go to Developer tab -> click on XML Mapping Pane. On the right-hand side of the document will be the XML Mapping menu, select “urn:microsoft-crm/document-template/contact/2/”.

Click on the empty space where you need to map the field as shown in the image below, right-click on the field name -> select Insert Content Control -> select Plain Text.

Map Doc - Step 1
Map Doc - Step 2

Once all the mapping is done, save the file.

Go back to Settings -> Templates -> Document Templates -> click on “Upload” and upload the template.

Create a Workflow that will run when the trigger field is updated and generate the word document in the Timeline of the contact form.

Go to Solutions Explorer -> select Process -> click on the “New” button -> give process name as “Generate Contact Document” -> select Category as “Workflow” -> select Entity as “Contact” -> Enable “Run this workflow in the background” check box and click OK.

The Workflow edit window appears, Enable the “Record fields change” checkbox in the Start when option under Options for Automatic Processes and select the “new_DocumentTrigger” field.

Go to Add Step -> select Perform Action from the dropdown. Give a name to the step and select “SetWordTemplate” from the Action dropdown -> let the Entity value be “None (global)” and click on the “Set Properties” button.

Create Workflow - Step 02

Once into the Properties, Lookup for the “Contact Document” word template for the SelectedTemplate field and {Contact(Contact)} for the Target field as shown in the below image.

Create Workflow - Step 3

Save and Activate the Workflow. Open the Contact form of a particular contact and click on the “Generate Contact Document” button on the ribbon. The process of updating the trigger field and triggering the workflow to run will be executed in the background and a Word Document will be generated in the Contact Timeline as shown in the below image.

B1 Result
Final Result

This tutorial can be used to trigger any kind of workflow using a Javascript code and a Button and without running it manually.

Tagged , , , , ,