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











up vote
0
down vote

favorite












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;
}
}

});









share|improve this question




















  • 1




    the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
    – bknights
    Nov 7 at 17:49






  • 1




    I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
    – Krypton
    Nov 7 at 20:01















up vote
0
down vote

favorite












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;
}
}

});









share|improve this question




















  • 1




    the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
    – bknights
    Nov 7 at 17:49






  • 1




    I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
    – Krypton
    Nov 7 at 20:01













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;
}
}

});









share|improve this question















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;
}
}

});






netsuite suitescript2.0 bfo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 12:56

























asked Nov 7 at 12:42









Jordan Johnson

64211233




64211233








  • 1




    the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
    – bknights
    Nov 7 at 17:49






  • 1




    I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
    – Krypton
    Nov 7 at 20:01














  • 1




    the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
    – bknights
    Nov 7 at 17:49






  • 1




    I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
    – Krypton
    Nov 7 at 20:01








1




1




the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
– bknights
Nov 7 at 17:49




the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are.
– bknights
Nov 7 at 17:49




1




1




I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
– Krypton
Nov 7 at 20:01




I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should.
– Krypton
Nov 7 at 20:01












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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;
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189688%2fwhy-is-this-advanced-pdf-template-not-populating-the-terms-from-the-object-that%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    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;
    }
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      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;
      }
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        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;
        }
        }





        share|improve this answer












        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;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 18:50









        ehcanadian

        5061818




        5061818






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189688%2fwhy-is-this-advanced-pdf-template-not-populating-the-terms-from-the-object-that%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Xamarin.form Move up view when keyboard appear

            JBPM : POST request for execute process go wrong