Search code examples
netsuitesuitescript

NetSuite - How to loop thru context newRecord objects when using User Event Script


When utilizing a User Event Script, I am attempting to loop through a context object to find all newRecord objects that are selected by a user in the Inventory Print screen. I am able to retrieve a single record within the context object, however, if a user selects multiple invoices to print, I am unable to retrieve the additional invoices.

When looking at the Execution log, I am unable to see the entire context object.

Invoice Selection

Invoice Rendering

My script is as follows:

/** @NApiVersion 2.0 @NScriptType UserEventScript */

define(['N/record'], function (record) { function beforeLoad(context) {

  try {

      var arrInvoiceIds= []
      var arrFileNames= []

      var r = context.newRecord
      var nrf = r.getFields()
      
      //fetch invoice record
    var invoiceRecord = record.load({ type: record.Type.INVOICE, id: r.id })
      
      for(var i=0; i < nrf.length; i++){
        
        var customerName = ""
        var invNo = ""
        var fileName = ""
        
        if(nrf[i] == "entity"){
          customerName = invoiceRecord.getValue('entity');
        }
        if(nrf[i] == "tranid"){
          invNo = invoiceRecord.getValue('tranid');
        }
        
        if(customerName != "" && invNo != ""){
          fileName = customerName + "_" + invNo;
        }
        
        
        
      }

      log.debug ({
          title: 'r.id',
          details: r.id //arrInvoiceIds //arrFileNames
      });
    
    log.debug ({
          title: 'nrf',
          details: nrf //arrInvoiceIds //arrFileNames
      });
    
    log.debug ({
          title: 'nrf.length',
          details: nrf.length //arrInvoiceIds //arrFileNames
      });
    
    log.debug ({
          title: 'context',
          details: context //arrInvoiceIds //arrFileNames
      });
    log.debug ({
          title: 'file',
          details: fileName //arrInvoiceIds //arrFileNames
      });

  } catch (e) {

      log.error ({ 
          title: e.name,
          details: e.message
      });  

  }
}

return {
  beforeLoad: beforeLoad      
}; });

Solution

  • When you print multiple invoices, the UserEvent is simply triggered multiple times. You should see that in your logs. The invoices are not combined on a single instance of context within a single UserEvent.