Power Community

Power Community

How to Create a Power Automate Flow to Populate a Word Template and Download the Result from a Model-Driven App?

If you want to create a button in a Model-Driven App to download a Word template in different formats (DOCX and PDF), follow these steps:

1. Create a Power Automate flow with an HTTP trigger.

EmadBeshai_0-1716709854762.png

 2.Add a step for Word template population.

EmadBeshai_1-1716710231525.png

3. After that check if you want it to be with docx format or pdf

EmadBeshai_2-1716710380591.png

4. Create new file for the populated word template on SharePoint as shown below

EmadBeshai_3-1716710459039.png

5. If you want to convert the file to PDF add the below step then create new file at the SharePoint from the word template content.

EmadBeshai_4-1716710542034.png

6. Get the file content from SharePoint and pass the values to get content step and pass the content to variable with the demonstrated expression

EmadBeshai_5-1716710921121.png

7. Finally pass the variable to the response of the flow

EmadBeshai_6-1716711020965.png

7. You can use the below JS code to download the File returned from the Power Automate Flow

function CallPrintFlow(flowURL, ids, FileType) {
(async () => {
					const rawResponse = await fetch(flowURL, {
						method: 'POST',
						headers: {
							'Accept': 'application/json',
							'Content-Type': 'application/json'
						},
						body: JSON.stringify(
							{
								id: ids,
								FileType: FileType
							})
					}).then(response => response.text())
						.then(data => {
							//_base64ToArrayBuffer(data);
							DownloadPDF(data, FileType);

						}).catch(Error => {
							Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");
							//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
							/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
							Xrm.Utility.closeProgressIndicator();
						});
				})();
			} else {
				console.log(this.responseText);
				Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");
				//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
				/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
				Xrm.Utility.closeProgressIndicator();
			}
		}
	};
	req.send();
}
function DownloadPDF(data,fileType) {
	let str = data;
	if (fileType == "PDF")
		var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".pdf";
	else if (fileType == "Word")
		var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".docx";
	var a = document.createElement("a");
	document.body.appendChild(a);
	a.style = "display: none";
	a.href = str;
	a.download = filename;
	a.click();
	Xrm.Utility.closeProgressIndicator();
}

If this post helps you with your problem, please give it a Thumbs Up. 

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

Create Brand Profiles in Customer Insights – Journeys to Create Cohesive Brand Experiences

In this blog, we will learn about one of the new features introduced by Microsoft in Dynamics 365 in...

More Articles Like This

- Advertisement -spot_img