Rendering an advanced PDF template on button action by including the resolveScript() in the userevent script.

First create a button using a userevent script to add a button. Use N/record, N/ui/serverWidget and N/url modules. The script sample is provided below:

const beforeLoad = (scriptContext) => {
            try {
                if (scriptContext.type === scriptContext.UserEventType.VIEW) {

                    // Create a button and add it to the form
                    let form = scriptContext.form;
                    let cashSale = scriptContext.newRecord;
                    let cashSaleId = cashSale.id;
                    //log.debug({ title: 'cash sale', details: cashSaleId })
                    if (cashSaleId) {
                        var scriptURL = url.resolveScript({
                            scriptId: 'customscript_jj_sl_customer_pick_slip', //script ID
                            deploymentId: 'customdeploy_jj_sl_customer_pick_slip', //deployment ID
                            params: {
                                cashSaleID: cashSaleId
                            }
                        });
    
                        form.addButton({
                            id: "custpage_customer_pick_slip",
                            label: "Customer Pick Slip",
                            functionName: 'window.open("' + scriptURL + '")'
                        });
                    }
                }
            } catch (error) {
                log.error("Error @ beforeLoad function", error)
            }
        }

We need to create a suitelet script for opening a suitelet page on button action. The script id and deployment id provided in the resolveScript function is the suitelet script id and deploment id. Use N/record, N/render, N/runtime, N/file modules. Suitelet script for rendering the advanced PDF template is provided below:

const onRequest = (scriptContext) => {
            try {
                if (scriptContext.request.method === 'GET') {
                    let cashSaleId = scriptContext.request.parameters.cashSaleID;
                    // Create a renderer for the advanced PDF template
                    if (cashSaleId) {
                        let renderer = render.create();
                        renderer.setTemplateById(216);
                        renderer.addRecord('record', record.load({
                            type: record.Type.CASH_SALE,
                            id: cashSaleId
                        }));
                        // Render the PDF
                        let pdfFile = renderer.renderAsPdf();
                        //Send the PDF as a response
                        scriptContext.response.writeFile({
                            file: pdfFile,
                            isInline: true
                        });
                    }
                }
            } catch (error) {
                log.error("Error @ onRequest function", error)
            }
        }

The created advanced PDF template with internal id 216 will be opened on the button action.

Leave a comment

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