//PDF generation API authored by Hassan Raza -- DGS/ETS/ESaaS function triggerFlow(recordid, data, flowUrl, fileName) { return new Promise((resolve, reject) => { if (typeof $ === "undefined") { console.error("jQuery not loaded"); reject("jQuery not loaded"); return; } console.log("Record ID:", recordid); console.log("Flow URL:", flowUrl); console.log("File Name:", fileName); let fileOpened = false; if (fileOpened) return; fileOpened = true; var payload = { eventData: JSON.stringify(data) }; console.log("Payload:", payload); shell.ajaxSafePost({ type: "POST", contentType: "application/json", url: flowUrl, data: JSON.stringify(payload), processData: false, global: false }).done(function(response) { console.log("Response received"); const result = JSON.parse(response); console.log("Parsed Result:", result); if (result.document_content) { console.log("Processing document content"); const byteCharacters = atob(result.document_content); const byteNumbers = Array.from(byteCharacters, char => char.charCodeAt(0)); const byteArray = new Uint8Array(byteNumbers); const blob = new Blob([byteArray], { type: 'application/pdf' }); const blobUrl = URL.createObjectURL(blob); var link = document.createElement("a"); link.href = blobUrl; link.download = fileName; link.style.display = "none"; document.body.appendChild(link); link.click(); document.body.removeChild(link); console.log("PDF download initiated"); resolve(true); } else { console.error("PDF data not found"); resolve(false); } }).fail(function(error) { console.error("Error occurred:", error); alert("An error occurred."); reject(error); }); }); }