Customized Word Template in CRM based on selected records
Word Template in Microsoft Dynamics CRM are used for creating word document dynamically for particular record. These documents help businesses visualize the data in CRM in different ways.
With Document template, Users can generate standardized Word documents with just one click.
Official documentation of using OOB word template - Word Template
There are certain limitations to the OOB word template provided by Dynamic CRM.
e.g., we cannot select particular records from a sub-grid and export only those records in our Word template.
To solve this issue, we can create a Word template and use that word template in power automate to populate the data of only selected records.
Please follow the below steps to achieve this functionality.
1. Create a Word document template and store it in SharePoint or OneDrive:
Open Word Document and insert Table and give header to table, e.g., I have given header as ID and Type.
In second row we want to add sub-grid data based on records selected. So, we will add text control and update properties of that control. I have added 2 control as text in bot columns
Since we want to insert row automatically if multiple records are selected, for that you can select second row of table and navigate to Developers>Control section of menu bar, there you will find option of "Repeating Section Content Control" option.
Once this word template is ready, save it and upload to SharePoint or OneDrive location so that we can access this template in power automate to perform operation.
2. Create Power Automate Flow with Power apps as a trigger and follow below steps.
- From Power apps we will send GUID of selected records. These GUID we will split by comma and store into the array "CaseRecords". We will also initialize "Case Summary" variable as a empty array.
- Apply to each loop will go through each record GUID. We will retrieve data of specific fields using "Get a Row by ID" action. After that we will append data into the array variable
- Use "Populate a Microsoft word template" action and select word template uploaded to SharePoint or OneDrive.[This we created as a first step]. In this action pass array data. Then You can send body of this action as a attachment to email.
- We can also create word document in SharePoint and then we can retrieve link of that SharePoint document and then pass that link in power apps response. With this URL we can use Download function of Power apps and user can download the word template rather than sending it to email
3. We will develop custom page which will call above power automate flow.
- We have created custom page which will be called by JavaScript on click of sub-grid button. This custom page will call power automate flow by passing all GUID's of selected records. we have added label on the page and one download button.
- On Start of app, we will get GUID's of selected record and store those GUID's in variable "caseRecords"
- On Click of Download button, we will call our flow and will pass all the GUID's. If we want to download the attachment then we will pass webUrl to power apps and that webUrl we can use it for downloading word document.
Our custom page is ready now. Now, we just need to call this custom page on click of sub-grid button. For that we can write JavaScript function to call this custom page on button click.
function downloadWordtemplate(selectedItems) {
var item = selectedItems;
let pageInput = {
pageType: "custom",
name: "new_wordtemplate_bb4a2",
entityName: "incident",
recordId: item,
};
let navigationOptions = {
target: 2,
width: 400,
height: 350,
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions)
.then(function () {
// Handle success
})
.catch(function (error) {
// Handle error
});
}
If you are not aware, selectedItems control is added via ribbon workbench.Once we call this function on sub-grid button, we will be able to download only selected records of sub-grid using our button.
OUTPUT:
Case Summary sub-grid
On Click of word template button, Custom Page will open:
On click of download button, User will received email of word template and word template will get download using our power automate flow.
NOTE: You can get option-set/lookup text value and pass those values in append to array action of power automate😊
Comments
Post a Comment