Cannot retrieve data from PO sublist into Inbound shipment sublist

47 views Asked by At

I try to add PO item sublist fields to inbound items sublist and my code doesn't work. Error:Cannot read properties of undefined (reading 'load')

function pageInit(scriptContext) {

  var purchaseOrder = scriptContext.record.load({
        type: record.Type.PURCHASE_ORDER,
        id: 'purchaseorder',
        isDynamic: true
    });

   var lines = scriptContext.purchaseOrder.getLineCount({
        sublistId: 'item'
    });
   var sample = scriptContext.purchaseOrder.getValue({
        fieldId: 'custcitem_po'
    });
    
   if (sample != '' && sample != null) { 
    for (var i=0; i<lines; i++){
        scriptContext.currentRecord.selectNewLine({
            sublistId: 'items'
                
            });


        scriptContext.currentRecord.setCurrentSublistValue({
            sublistId: 'items',
            fieldId: 'custrecord_sample',
            value: sample,
            ignoreFieldChange: true
        });

        scriptContext.currentRecord.commitLine({
            sublistId: 'items'
            
        });
    }

}
}

updates: I understand your point and I modified the code:

function pageInit(scriptContext) {

    var purchaseOrder = record.load({
        type: record.Type.PURCHASE_ORDER,
        id: '1234', //works only if I specify the exact ID of a purchase order
        isDynamic:true
      }); 
        
      var lines = purchaseOrder.getLineCount({
        sublistId: 'item'
            
      }); 
      if (lines>=0){
          for (var i=0; i<lines; i++){
            scriptContext.currentRecord.selectNewLine({
              sublistId: 'items'
            });
            scriptContext.currentRecord.setCurrentSublistValue({
              sublistId: 'items',
              fieldId: 'custrecord_sample',
              value:purchaseOrder.getSublistValue({
                sublistId:'item',
                fieldId: 'custcitem_po',
                line:i
              }),
              ignoreFieldChange: true
            });
            
            scriptContext.currentRecord.commitLine({
              sublistId: 'items'
            });
          }
        }   
}

The code works well if the id of the PO is mentioned however, I would like it to be valid for any id. Any idea how to do?

2

There are 2 answers

1
d.k On

According to the docs (not sure if the link will work netsuite.com/app/help/helpcenter.nl?fid=section_4410597671.html, so you can just use the global search for 'pageInit(scriptContext)') the scriptContext argument doesn't have a record property. Only the currentRecord.

Also most likely the scriptContext doesn't have the purchaseOrder either.

Try this, sorry, I couldn't test it of course, not that simple. If it doesn't work, post a comment please.

function pageInit(scriptContext) {
  'use strict';
  var purchaseOrder = scriptContext.currentRecord.get();

  var lines = purchaseOrder.getLineCount({
    sublistId: 'item'
  });

  var sample = purchaseOrder.getValue({
    fieldId: 'custcitem_po'
  });

  if (sample != '') {
    for (var i = 0; i < lines; i++){
      scriptContext.currentRecord.selectNewLine({
        sublistId: 'items'
      });

      scriptContext.currentRecord.setCurrentSublistValue({
        sublistId: 'items',
        fieldId: 'custrecord_sample',
        value: sample,
        ignoreFieldChange: true
      });

      scriptContext.currentRecord.commitLine({
        sublistId: 'items'
      });
    }
  }
}
0
tengiyo On

In order for your code to work with any PO, you can set the id to the currentRecord's id:

var purchaseOrder = record.load({
    type: record.Type.PURCHASE_ORDER,
    id: scriptContext.currentRecord.id,
    isDynamic:true
  });

However, it will only work for already existing PO's which you are editing. At the time of PO creation there is no id assigned to the current record yet, therefore no PO to load. As pageInit is a client script function, it will only run when you create/edit a record in user interface.

Futhermore, if you are attempting to work with the current PO, you don't need to load it through record.load(), because the currentRecord refers to the record you are already at.

So all you would really need is:

var purchaseOrder = scriptContext.currentRecord;