I am trying to write a scheduled script to run eac...
# suitescript
m
I am trying to write a scheduled script to run each night. It will take any Transfer Orders that are NOT received in NetSuite but ARE received in Amazon and receive each line item. My code is as follows:
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 */
 define(['N/search','N/record'], function (s,r) {
	function execute(context) {
      var approvalSearch = s.load({
        id: 'customsearch_to_tbreceived'
      });
      var resultSet = approvalSearch.run();
      resultSet.each(processTransferOrder);
	};

	function processTransferOrder(result){
		var internalID = result.getValue(result.columns[0]);
      	log.debug({title:'InternalID',details:internalID});
      	var record = r.load({
			type:'transferorder',
			id:internalID
		});

      	var irRecord = r.transform({
          fromType: 'transferorder',
          fromId: internalID,
          toType: 'itemreceipt'
        });

      	var itemCount = irRecord.getLineCount('item');

      	for (var i = 0; i < itemCount; i++){
          var itemId = irRecord.getSublistValue('item','item',i);
          var quantity = irRecord.getSublistValue('item','quantity',i);
          var lineId = irRecord.getSublistValue('item','line',i);
          var location = irRecord.getSublistValue('item','location',i);
          log.debug({
            title:'Item - Quantity - LineId - Location',
            details:itemId + ' - ' + quantity + ' - ' + lineId + ' - ' + location
          });
          irRecord.setSublistValue({
            sublistId: 'item',
            fieldId: 'itemreceive',
            line: lineId,
            value:true
          });
        }

      	var idl = irRecord.save({
          enableSourcing: true
        });
        return true;
	};
	return({
		execute:execute
	});
 });
When it runs, it gives the following error:
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "UNEXPECTED_ERROR",
  "message": null,
  "stack": [
    "anonymous(N/serverRecordService)",
    "processTransferOrder(/SuiteScripts/bb_auto_receive_to.js:39)",
    "each(N/searchObject)",
    "execute(/SuiteScripts/bb_auto_receive_to.js:11)"
  ],
  "cause": {
    "type": "internal error",
    "code": "UNEXPECTED_ERROR",
    "details": null,
    "userEvent": null,
    "stackTrace": [
      "anonymous(N/serverRecordService)",
      "processTransferOrder(/SuiteScripts/bb_auto_receive_to.js:39)",
      "each(N/searchObject)",
      "execute(/SuiteScripts/bb_auto_receive_to.js:11)"
    ],
    "notifyOff": false
  },
  "id": "853f25fd-e6ab-452a-9ffb-13ced90c4b5c-2d323032322e30362e3238",
  "notifyOff": false,
  "userFacing": false
}
It gives this error on line 39 on the 2nd iteration through the items. Anyone see what I am doing wrong?
b
its not required for the line id to be the same as the line number
m
Should 'line' always be equal to 'i'?
b
yes
m
Thanks, testing now
I never followed up, but this worked! Thanks so much!