Download PDFs by using “Download PDF button” in Transaction forms

UserEvent Script

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/recordContext', 'N/url'],
    /**
     * @param{record} record
     * @param{recordContext} recordContext
     * @param{url} url
     */
    (record, recordContext, url) => {
       
        const beforeLoad = (scriptContext) => {
            try {
            if(scriptContext.type=="view" || scriptContext.type== "edit" ) {
                scriptContext.form.clientScriptModulePath ='SuiteScripts/Jobin and Jismi IT Services LLP/APC-369/JJ CS Download transaction PDF on button click APC-369.js';
                    scriptContext.form.addButton({
                        id: 'custpage_buttons',
                        label: 'Download PDF',
                        functionName: 'downloadPDF'
                    });


                }
            }catch (err){
                log.debug('error',err)
            }
        }

        return {beforeLoad}

    });


Client Script

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord', 'N/record', 'N/recordContext', 'N/url'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{recordContext} recordContext
     * @param{url} url
     */
    function(currentRecord, record, recordContext, url) {


        function pageInit(scriptContext) {
        }
        function downloadPDF(){
            try {
                var currentRec=currentRecord.get().id;
                console.log('currentRec',currentRec)
                var doc= currentRec.tranid;

                var createLinkUrl = url.resolveScript({
                    scriptId: 'customscript_jj_sl_download_pdf_apc_369',
                    deploymentId: 'customdeploy_jj_sl_download_pdf_apc_369',
                    returnExternalUrl: false,
                    params: {
                        currentRec: currentRec,
                    }

                });
                window.open(createLinkUrl);


            }catch (e) {
                log.debug(e)
            }
        }
        return {
            pageInit: pageInit,
            downloadPDF: downloadPDF

        };

    });


Suitelet Script

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/record', 'N/recordContext', 'N/redirect', 'N/render', 'N/runtime', 'N/url','N/search'],
    /**
     * @param{record} record
     * @param{recordContext} recordContext
     * @param{redirect} redirect
     * @param{render} render
     * @param{runtime} runtime
     * @param{url} url
     */
    (record, recordContext, redirect, render, runtime, url,search) => {
       
        const onRequest = (scriptContext) => {
            try {
                    var rec = scriptContext.request.parameters;
                    var recId = rec.currentRec;

                    var docNum = search.lookupFields({
                        type:"transaction",
                        id: recId,
                        columns: ["tranid"]
                    }).tranid;
                    log.debug("docNum",docNum)

                        var downloadPdf = render.transaction({
                            entityId: Number(recId),
                            printMode:render.PrintMode.PDF,
                            inCustLocale: true
                        })

                    downloadPdf.name = (docNum || Date.now())+ ".pdf";
                    scriptContext.response.writeFile(downloadPdf, false)
            }catch (e) {
                log.debug('Error @onRequest',e);
            }

        }

        return {onRequest}

    });





Leave a comment

Your email address will not be published. Required fields are marked *