netsuitesuitescript2.0bfo

Why is this Advanced PDF template not populating the terms from the object that I am passing to it?


So, basically I am trying to get the XML template for the PDF and I plan to eventually add some additional XML in the code after getting the template working in this manner. However, when I attempt to pass a data source object to the PDF it is not working. Does anyone know the cause of this issue, and what I am doing incorrectly here?

XML template (stripped out everything but the variable in the table for testing):

<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<!--removed lengthy head to make code more readable-->
<body footer="nlfooter" footer-height="20pt" padding="0.5in 0.5in 0.5in 0.5in" size="Letter">
    <table class="body" style="width: 100%; margin-top: 10px;">
    <tr>
    <td>${jsonObj.terms}</td>
    </tr></table>
</body>
</pdf>

Script:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/error','N/render','N/file','N/record','N/log'],
/**
 * @param {error} error
 */
function(error, render, file, record, log) {

    function beforeSubmit(context) {

        log.debug('After submitting invoice, create advanced PDF detail layout', context);

        var isInvoice = context.newRecord.type == 'invoice';

        // Create advanced PDF
        if (isInvoice){
            log.audit('Creating invoice');

            renderRecordToPdfWithTemplate(context.newRecord);

        }
        else{
            error.create({
                name: 'ERROR_RECEIVED',
                message: 'Cannot create advanced PDF from this record type'
            });

            log.audit(error.name,error.message);
        }

    }

    return {
        beforeSubmit: beforeSubmit
    };

    function renderRecordToPdfWithTemplate(context) {

        var jsonObj = {
            terms: "test terms"
        };

        var templateId = '7959'; // ID of the XML
        var templateFile = file.load({id: templateId});
        var renderer = render.create();
        renderer.templateContent = templateFile.getContents();

        /*
        renderer.addRecord({
            type: record.Type.INVOICE,
            record: context
        });
        */

        renderer.addCustomDataSource({
            format: render.DataSource.OBJECT,
            alias: "jsonObj",
            data: jsonObj
        });

        log.debug('Rendering as PDF');

        var renderXmlAsString = renderer.renderAsString();
        log.debug('Added record to PDF', context);

        var invoicePdf = render.xmlToPdf({
            xmlString: renderXmlAsString
        });

        invoicePdf.name = 'Testpdf2.pdf';
        invoicePdf.folder = -15;

        try{
            var fileId = invoicePdf.save();
            log.debug('Saved PDF to file '+fileId);
        }
        catch(e){
            alert('Error saving file');
            log.debug('Error saving file');
            debugger;
        }
    }

});

Solution

  • You don't need the renderer.renderAsString(); since you're already loading the XML from the file cabinet.

    function renderRecordToPdfWithTemplate(context) {
    
        var jsonObj = {
            terms: "test terms"
        };
    
        var templateId = '7959'; // ID of the XML
        var templateFile = file.load({id: templateId});
        var renderer = render.create();
        renderer.templateContent = templateFile.getContents();
    
        renderer.addCustomDataSource({
            format: render.DataSource.OBJECT,
            alias: "jsonObj",
            data: jsonObj
        });
    
        log.debug('Rendering as PDF');
    
        var invoicePdf = renderer.renderAsPdf();
        invoicePdf.name = 'Testpdf2.pdf';
        invoicePdf.folder = -15;
    
        try{
            var fileId = invoicePdf.save();
            log.debug('Saved PDF to file '+fileId);
        }
        catch(e){
            alert('Error saving file');
            log.debug('Error saving file');
            debugger;
        }
    }