Render PDF template to source the taxcode and VAT% using Suitlet

Render PDF template to source the taxcode and VAT% values

Suitlet:

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
 define(['N/record', 'N/render', 'N/search', 'N/file', 'N/redirect', 'N/xml', 'N/url'],

 function ( record, render, search, file, redirect, xml, url) {

     function vgmRecord(context) {
		 
         try {
             // var vgm = context.request.parameters.vgmid;
             log.debug("vgmid");
             var recId = context.request.parameters.Id;
              var recIdObj = record.load({
                type: record.Type.INVOICE,
                id: recId,
                isDynamic: true,
            });
           log.debug("recIdObj",recIdObj);
             var transactionSearchObj = search.create({
                 type: "transaction",
                 filters:
                 [
                   ["type","anyof","CustInvc"],
      				"AND",
      					["internalid","anyof",recId],
                   "AND",
      					["mainline","is","F"],
      				"AND",
      					["taxline","is","F"],
      				"AND",
     	 				["cogs","is","F"]
                 ],
                 columns:
                 [
                    search.createColumn({
                       name: "taxcode",
                       summary: "GROUP",
                       label: "Tax Item"
                    }),
                    search.createColumn({
                       name: "rate",
                       join: "taxItem",
                       summary: "GROUP",
                       sort: search.Sort.ASC,
                       label: "Rate"
                    })
                 ]
              });
            

              var taxArray = [];
              transactionSearchObj.run().each(function (result) {
                  var Taxcode = result.getText(transactionSearchObj.columns[0]);
                  var TaxRate = result.getValue(transactionSearchObj.columns[1]);
                  var taxObj = {taxcode: Taxcode, rate: TaxRate};
                  taxArray.push(taxObj);
                  
                  return true;
              });
              var taxlistObj = {taxArray: taxArray};

              log.debug("Job task array",taxArray);
           log.debug("Job task array1",taxlistObj);
            //   var fieldLookUp = search.lookupFields({
            //type:search.Type.INVOICE,
            //id:recId,
           // columns:['customform']
          //});
            //   var customForm = fieldLookUp.customform[0].value;

              //var recIdObj = context.request.parameters.recordID;
              
           		var customForm = context.request.parameters.customForm
          //   var TaxCode = equipmentList(customerId)
             
                 if(customForm==148)
                 { 
                  var renderer1 = render.create();
                        //var transactionFile = render.transaction({
                         //  entityId: 41683,
                          // formId:148,
                          // printMode: render.PrintMode.HTML,
                           //inCustLocale: true
                           //});

                    var template = 114;
                   renderer1.setTemplateById(template);
                    log.debug("renderer1",renderer1);
                renderer1.addRecord('record', recIdObj);
                 renderer1.addCustomDataSource({
                     format: render.DataSource.OBJECT,
                     alias: "JSON",
                     data: taxlistObj
                 });
                 //renderer1.setTemplateByScriptId("CUSTTMPL_114_6450344_784");
                 var invoicePdf = renderer1.renderAsPdf();
                 context.response.writeFile(invoicePdf,true);
                     //window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=148&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
                 }
                else
                 {
                     var renderer2 = render.create();
                     log.debug("renderer2",renderer2);
                     renderer2.addRecord('record', recIdObj);
                     renderer2.addCustomDataSource({
                         format: render.DataSource.OBJECT,
                         alias: "JSON",
                         data: taxlistObj
                     });
                     renderer2.setTemplateByScriptId("CUSTTMPL_113_6450344_320");
                     var invoicePdf1 = renderer2.renderAsPdf();
                     context.response.writeFile(invoicePdf1,true);
                       //window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=145&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
                 }
             
         }


         catch (e) {
             log.debug('catch error::', e)

         }
     }

     var main = {

         onRequest: function (context) {

             vgmRecord(context)

         }
     };
     return main;

 });

 
In Advanced PDF Template: 
<#if JSON?has_content>
<#if JSON.taxArray?has_content>
 <table>
   <thead>
      <tr> 
         <td align="left" colspan="5">TaxCode</td>
         <td align="left" colspan="5">VAT%</td>
        </tr>
   </thead>
  <#list JSON.taxArray item>
        <tr>
            <td align="left" colspan="5">${item.taxcode}</td>
            <td align="left" colspan="5">${item.rate}</td>
        </tr>
    </#list>
   </table>
  </#if>
</#if>

Leave a comment

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