How to generate PDF file for transaction record against its specific form template in SuiteScript 2.0

In this post, I will share how to generate a PDF document for a transaction record in a suitelet using suitescript 2.0. Definitely to understand this post, you must have some understanding of SuiteScript 2.0 and Suitelets. To make it simple I am using hard coded values for transaction id and transaction form template id.

var tranId = 7626;
var customTranTemplateId = 126;

To get the transaction's form template id, you will need to navigate to the main menu than Customization → Forms → Transaction Forms and copy the internal id of template (in my case it is sales order's customize form id).



Create a TemplateRenderer object.

var renderer = render.create();

Now, the important step is to load the transaction record and merge it with template content.

var transactionFile = render.transaction({
                    entityId: tranId,
                    printMode: render.PrintMode.HTML,
                    formId: customTranTemplateId
                });

Note : In above function if formId is not defined then render module by default select the standard form template of transaction.

Last step is to write it to the response to download the PDF.

 response.writeFile(transactionFile);


Complete Code:

/**
 *@NApiVersion 2.0
 *@NScriptType Suitelet
*/
define(['N/render', 'N/record'],
    function (render, record) {
        function onRequest(options) {
            try {
                var tranId = 7626;
                var customTranTemplateId = 126;

                var response = options.response;
                var renderer = render.create();
                var transactionFile = render.transaction({
                    entityId: tranId,
                    printMode: render.PrintMode.HTML,
                    formId: customTranTemplateId
                });

                response.writeFile(transactionFile);
            } catch (e) {
                log.error({
                    title: 'Error : ' + e.name,
                    details: e.message
                });   
            }
        }

        return {
            onRequest: onRequest
        };

    });

Comments

  1. Thanks! This helps me understand the way printing templates are used with custom forms so much better than the documentation.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create a sample ASP core application with vue.js in visual studio