How to post record that have sublist like item in ...
# suitescript
m
How to post record that have sublist like item in RESTlet? I try using this code, and it work well with customer record because I only need to input companyName and email, but when I try to input inventory adjustment, it always give an error
You must enter at least one line item for this transaction.
Copy code
function post(context) {
         doValidation([context.recordtype], ['recordtype'], 'POST');
         var rec = record.create({
             type: context.recordtype
         });
         for (var fldName in context)
             if (context.hasOwnProperty(fldName))
                 if (fldName !== 'recordtype')
                     rec.setValue(fldName, context[fldName]);
         var recordId = rec.save();
         return String(recordId);
     }
r
So basically what NetSuite is telling you that you need to add at least one item on this record. Inventory Adjustment records adjust qty for INV items. There is no mean of creating one until you pass an item.
e
The issue you are facing may be related to the sublistId. You are looking for “inventory” (not “item”) I believe on an Inv Adj.
m
I try using this code
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */

 define([
  'N/record',
], function(record) {
  function doPost(data){
  var recordObj = record.create({
      type: "inventoryadjustment",
      isDynamic: true
  });

  var account = data.account;
  var department = data.department;
  var adjlocation = data.adjlocation;
  var items = data.items;

  recordObj.setValue({
      fieldId:'account',
      value:account
  });

  recordObj.setValue({
    fieldId:'department',
    value:department
  });
  
  recordObj.setValue({
    fieldId:'adjlocation',
    value:adjlocation
  });


  for (i = 0; i < items.length; i++) {
  recordObj.selectNewLine({
      sublistId: 'item'
      });

      recordObj.setCurrentSublistValue({
      sublistId: 'item',
      fieldId: 'item',
      value: items[i][0]
      });

      recordObj.setCurrentSublistValue({
      sublistId: 'item',
      fieldId: 'quantity',
      value: items[i][1]
      });

      recordObj.commitLine({
      sublistId:'item'
      });
  }
      var recordId = recordObj.save({
          enableSourcing: false,
          ignoreMandatoryFields: false
          });

      return recordId;

  }
  return {post:doPost};
});
And get an error message :
Copy code
{
    "error": {
        "code": "TypeError",
        "message": "TypeError: Cannot read property \'length\' of undefined [at Object.doPost (/SuiteScripts/restlet_invadj.js:37:19)]"
    }
}
I try change the type to this and still get the same error
Copy code
var recordObj = record.create({
      type: record.Type.INVENTORY_ADJUSTMENT,
      isDynamic: true
  });
I try to change the code, and still get the same error, any info on the TypeError?
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */

 define([
  'N/record',
], function(record) {
  function doPost(data){
  var recordObj = record.create({
      type: record.Type.INVENTORY_ADJUSTMENT,
      isDynamic: true
  });


  var account = data.account;
  var department = data.department;
  var adjlocation = data.adjlocation;
  var items = data.items;
  var id = id;
  var quantity = quantity;
  var rate = rate;
 
  recordObj.setValue({
    fieldId:'account',
    value:account
  });

  recordObj.setValue({
    fieldId:'department',
    value:department
  });
  
  recordObj.setValue({
    fieldId:'adjlocation',
    value:adjlocation
  });


  for (i = 0; i < items.length; i++) {
  recordObj.selectNewLine({
      sublistId: 'item'
      });

      recordObj.setCurrentSublistValue({
      sublistId: 'item',
      fieldId: 'id',
      value: items[i].id
      });

      recordObj.setCurrentSublistValue({
      sublistId: 'item',
      fieldId: 'quantity',
      value: items[i].quantity
      });

      recordObj.setCurrentSublistValue({
      sublistId: 'item',
      fieldId: 'rate',
      value: items[i].rate
      });

      recordObj.commitLine({
      sublistId:'item'
      });
  }
      var recordId = recordObj.save({
          enableSourcing: false,
          ignoreMandatoryFields: false
          });

      return recordId;

  }
  return {post:doPost};
});
r
data.items
is undefined. It is a normal JS error.
m
But I have defined it at above?