Rendering an advanced PDF template on button action.

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

const beforeLoad = (scriptContext) => {
            if (scriptContext.type === scriptContext.UserEventType.VIEW) {
               
 // Create a button and add it to the form
                let form = scriptContext.form;
                let salesOrder = scriptContext.newRecord;
                let salesOrderId = scriptContext.newRecord.id;
                //log.debug({ title: 'sales order', details: salesOrderId })
                form.addButton({
                    id: "custpage_button_id",
                    label: "Button Label",
                    functionName: 'window.open("/app/site/hosting/scriptlet.nl?script=1295&deploy=1&salesOrderID=' + salesOrderId + '")'
                });
            }
}

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

const onRequest = (scriptContext) => {
            
                if (scriptContext.request.method === 'GET') {

                
let salesOrderId = scriptContext.request.parameters.salesOrderID;
                        // Create a renderer for the advanced PDF template
                        let renderer = render.create();
                        renderer.setTemplateById(111);//here 111 is the internal id of the template 
                        renderer.addRecord('record', record.load({
                            type: record.Type.SALES_ORDER,
                            id: salesOrderId
                        }));
                        // Render the PDF
                        let pdfFile = renderer.renderAsPdf();
                        //Send the PDF as a response
                        scriptContext.response.writeFile({
                            file: pdfFile,
                            isInline: true
                        });
}

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

Leave a comment

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