oraclenetsuiteerp

Printing copy/original (Netsuite)


how could I print original document when printing first time, and a copy whenever I print after the first time?


Solution

  • When printing a document, a UserEvent is triggered.

    You can store a timestamp when it's first printed (or a simple checkbox) on the document.

    You can either print the original document or the copy depending on the timestamp/checkbox.

    /**
     *@NApiVersion 2.x
     *@NScriptType UserEventScript
    */
    define(['N/record'], function(record) {
    
      function beforeLoad(context) {
        var UserEventType = context.UserEventType;
        var contextType = context.type;
        var newRecord = context.newRecord;
    
        if (contextType == UserEventType.PRINT) {
          var fieldId = 'custrecord_is_printed' // fieldId of your custom field / checkbox (or use a datetimestamp)
    
          var isPrinted = newRecord.getValue({ fieldId: fieldId })
          if (!isPrinted) {
            var myRecord = record.load({ type: newRecord.type, id: newRecord.id }) // in the beforeLoad, editing the newRecord is not allowed, so you need to load the record first, edit and save.
            myRecord.setValue({ fieldId: fieldId, value: true })
            myRecord.save()
          }
        }
      }
    
      return {
          beforeLoad: beforeLoad,
      }
    
    });
    

    in your advanced html template use a condition like

    <#if record.custrecord_is_printed>
      <p>COPY</p>
    <#else>
      <p>ORIGINAL</p>
    </#if>