Nadav Julius
Nadav Julius

Reputation: 311

NetSuite: How can I add a record to a Advanced PDF/HMTL Template?

So I know that I can use the N/render to generate a template and I can use the addRecord to add record objects to the print template to make them available in the FTL.

My question is if I can do something similar when the native print button is clicked and prints a Advanced PDF/HTML Template. I know that I can catch the PRINT event in the User Event script but beyond that I am stuck.

I know the question is a little general I will add context on request. I just don't know which way to go.

EDIT: I am familiar with the option of adding a custpage field to the form and then extracting the JSON in the FTL. In this specific situation it would be much more convenient if I could simply add a full record. Meaning I am on a Item Fulfillment print and want to add the FULL parent Sales Order record to the print so that I can access it in the FTL by salesorder.memo etc. Something similar to:

require(['N/render'], function(render) {
   var renderer = render.create();
   renderer.addRecord('customer', record.load({ type: record.Type.CUSTOMER, id: customer }));
})

The issue is that I only know how to do this for completely custom prints but not prints that are printed from the Native print buttons on transactions. I need this to do line matching from the Sales Order lines to the Item Fulfillment lines and would rather do it this way if possible instead of creating a custpage and inserting a custom made object.

Upvotes: 1

Views: 3407

Answers (2)

SuiteStar
SuiteStar

Reputation: 132

As per your question you want to add item sublist data also from sales order on print of item fulfillment. if it is so, then here I have used for same situation.

Steps:

  • Write a user event before load script on print mode only and then create a saved search to get the data of item and save it in custom field with long text type with space as label.
  • Customize your standard pdf template that is attached to item fulfillment record.
    GoTo- customization- forms- Advanced pdf template-Customize preferred template for item fulfillment.
  • Add a table there with that custom field.

It will work on standard print button. I have done it for work order record. You may edit in search using sales order saved search.

UserEvent

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
 define(['N/record', 'N/search', 'N/ui/serverWidget'], function (record, search, serverWidget) {
    function beforeLoad(scriptContext) {
       try {
 
          if (scriptContext.type == 'print') {
             var currentRec = scriptContext.newRecord;
             var recid = currentRec.id;
 
             columns[0] = search.createColumn({
                name: "sequence",
                join: "manufacturingOperationTask",
                sort: search.Sort.ASC,
                label: "Operation Sequence"
             });   
 
             columns[1] = search.createColumn({
                name: "custevent_custom_op_name",
                join: "manufacturingOperationTask",
                label: "Operation Name(Instruction)"
             });
             columns[2] = search.createColumn({
                name: "manufacturingworkcenter",
                join: "manufacturingOperationTask",
                label: "Manufacturing Work Center"
             });
 
             columns[3] = search.createColumn({
                name: "formulanumeric",
                formula: "Round({manufacturingoperationtask.runrate}*{quantity}/60,2)",
                label: "BudgetHours"
             });
 
            
             //Creating search to get all the values for work order
             var mySearch = search.create({
                type: "workorder",
                filters:
                   [
                      ["type", "anyof", "WorkOrd"],
                      "AND",
                      ["internalid", "anyof", recid],
                      "AND",
                      ["mainline", "is", "T"]
 
                   ],
                columns: columns
             });
             var searchResultCount = mySearch.runPaged().count;

             mySearch.run().each(function (result) {
                // .run().each has a limit of 4,000 results
                
                results.push(result);
                return true;
             });
             //populate current printout with custom record entries
             var customRecords = { columns: columns, results: results };
 
             var columns = customRecords.columns, results = customRecords.results;
 
             var custrecord = scriptContext.form.addField({ id: 'custpage_custrecord_to_print', type: serverWidget.FieldType.LONGTEXT, label: " " }),
                custrecordArray = [];
 
             if (results && results instanceof Array) {
 
                for (var i = 0; i < results.length; i++) {
 
                   var singleLine = {};
                   for (var j = 0; j < columns.length; j++) {
                      if (i == i && j == 2) {
                         var value = results[i].getText(columns[j]);
 
 
                      } else {
                         var value = results[i].getValue(columns[j]);
 
                      }
 
                      if (j == 0 || j == 1 || j == 2) {
                         if (value.indexOf('.') == 0 || value.indexOf(',') == 0 || value.indexOf('-.') == 0 || value.indexOf('-,') == 0) {
                            value = '0' + value;
 
                         }
 
                      }
                      singleLine["col" + j] = (value) ? value : '';
 
                   }
                   custrecordArray.push(singleLine);
                }
                custrecord.defaultValue = JSON.stringify(custrecordArray);
 
             }

 
          }
 
       } catch (e) {
          log.error("ERROR", e);
       }
    
    
 
    }
 
    return {
       beforeLoad: beforeLoad,
    };
 });

In Advanced Pdf Template:-

<#if record.custpage_custrecord_to_print?has_content>
  <#assign customrecord = record.custpage_custrecord_to_print?eval />
  <table width="100%" class="second_table" style="page-break-inside: auto; width: 100%; margin-top: 2px; padding-top: 0px">
    <#list customrecord as customrecord_line>
      <tr width="100%" border-top="solid black" margin-top="10px" style="margin-top:10px; page-break-before: auto;">
          <th width="25%" align="left" style="padding: 2px 2px;">Step</th>
          <th width="25%" align="center" style="padding: 2px 2px;">Activity</th>
        <th width="25%" align="center" style="padding: 2px 2px;">Run Rate(Min/Unit)</th>
          <th width="25%" align="center" style="padding: 2px 2px;">BudgetHours</th></tr>
    <tr width="100%" style="page-break-inside: auto;">
        <td width="25%" align="left" style="padding: 2px 2px;">0${customrecord_line.col0}</td>
        <td width="25%" align="center" style="padding: 2px 2px;">${customrecord_line.col2}</td>
      <td width="25%" align="center" style="padding: 2px 2px;">${customrecord_line.col3}</td>
        <td width="25%" align="center" style="padding: 2px 2px;">${customrecord_line.col4}</td>
     </tr>
        
</list>
</table>
</#if>

It will be helpful.

Thanks,

Upvotes: -1

W.S.
W.S.

Reputation: 1505

I refer to one of my previous answer.

Use the beforeLoad hook in a UserEventScript to set extra data on the context.form. You'll be able to access this data on the template.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
*/
define(['N/ui/serverWidget'], function(serverWidget) {

  function beforeLoad(context) {

    // var request = context.request;
    // var newRecord = context.newRecord;

    var form = context.form;
    var type = context.type;
    var UserEventType = context.UserEventType;

    // only execute during printing...
    if (type != UserEventType.PRINT) return
    
    var customData = {
       hello: 'world'
    }

    var field = form.addField({
      id : 'custpage_custom_data',
      label: 'Custom Data',
      type : serverWidget.FieldType.LONGTEXT
    });

    field.defaultValue = JSON.stringify(customData); 

  }

  return {
    beforeLoad: beforeLoad
  };

})

You can access the data within the template through:

<#if record.custpage_custom_data?has_content>
<#assign custom_data = record.custpage_custom_data?eval />
</#if>

Upvotes: 0

Related Questions