Hello, I try using Restlet to Post Inventory Adjus...
# suitescript
m
Hello, I try using Restlet to Post Inventory Adjustment But get an error :
"You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist
Any info on this error? I included my code and json in the reply
Here is my code :
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */

 define([
  'N/record',
], function (record) {
  function doPost(requestBody) {

  log.debug('Post body', requestBody);

  var inventoryAdjustment = record.create({
      type: record.Type.INVENTORY_ADJUSTMENT,
      isDynamic: true
  });

  inventoryAdjustment.selectNewLine({
      sublistId: 'item'
  });

  var items = requestBody.items;
  items.forEach(function (item) {

      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'item',
          value: item.item_id 
      });

      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'quantity',
          value: item.quantity
      });
      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'rate',
          value: item.rate 
      });

      inventoryAdjustment.commitLine({
          sublistId: 'item'
      });
  });

  try {
      var id = inventoryAdjustment.save({
          ignoreMandatoryFields: false
      });
      log.debug('record save with id', id);

      return id;
  } catch (e) {
      return 0;
  }
}

return {
  post: doPost
  };
});
Here is my json :
Copy code
{
  "account": 113,
  "department": 102,
  "adjlocation": 103,
  "item": {
    "items": [
      {
        "item": {
          "id": 3569
        },
        "rate": 20000,
        "quantity": 5
      }
    ]
  }
}
b
basically what the error says
there is no sublist with an id of item on an inventory adjustment
m
so, what the sublist id if I want to insert item on inventory adjustment?
b
load an existing inventory adjustment so that you can inspect it to see its ids
m
inventory?
I get this data when I load an existing inventory adjustment
b
that screenshot shows 2 sublists, one with an id of inventory, the other with recmachcustrecordinvoice_number
m
Tried change the code to this :
Copy code
inventoryAdjustment.selectNewLine({
      sublistId: 'inventory'
  });


  var items = requestBody.items;
  items.forEach(function (inventory) {

      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'inventory',
          fieldId: 'item',
          value: inventory.item //internal id 
      });

      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'inventory',
          fieldId: 'quantity',
          value: inventory.quantity
      });
      inventoryAdjustment.setCurrentSublistValue({
          sublistId: 'inventory',
          fieldId: 'rate',
          value: inventory.rate //custom in netsuite
      });

      inventoryAdjustment.commitLine({
          sublistId: 'inventory'
      });
  });

  try {
      var id = inventoryAdjustment.save({
          ignoreMandatoryFields: false
      });
      log.debug('record save with id', id);//sales order internal id

      return id;
  } catch (e) {
      return 0;
  }
}
Now it showed an error :
TypeError: Cannot read property \'forEach\' of undefined
b
thats a normal javascript error
its saying that your request body does not have a key named items
m
I use this record data for that code :
Copy code
{
  "account": 113,
  "department": 102,
  "adjlocation": 103,
  "inventory": {
    "items": [
      {
        "item": 3569,
        "rate": 20000,
        "quantity": 5
      }
    ]
  }
}
b
lookup how hierarchies work in json and javascript objects
requestBody has 4 keys, none of which are items
m
Copy code
"inventory": {
            "currentline": {
                "adjustqtyby": "",
                "avgunitcost": "",
                "costingmethod": "",
                "cseg_brands": "",
                "cseg_category": "",
                "currency": "",
                "currentvalue": "",
                "custcol_itemcode": "",
                "description": "",
                "exchangerate": "1",
                "foreigncurrencyunitcost": "",
                "invtdiffvalue": "",
                "item": "",
                "label": "",
                "labelcurrency": "",
                "labelfxunitcost": "",
                "line": "",
                "location": "",
                "memo": "",
                "newquantity": "",
                "quantityonhand": "",
                "sys_id": "-3017117072737655",
                "sys_parentid": "3017116882548740",
                "unitconversionrate": "",
                "unitcost": "",
                "units": "",
                "#": "2"
            },
            "line 1": {
                "adjustqtyby": "20",
                "avgunitcost": "100.00",
                "costingmethod": "AVG",
                "cseg_brands": "234",
                "cseg_brands_display": "WYETH",
                "cseg_category": "109",
                "currency": null,
                "currentvalue": "0.00",
                "custcol_itemcode": "8886472103133",
                "description": "S26 PROCAL GOLD 1600 GR",
                "exchangerate": "1",
                "foreigncurrencyunitcost": null,
                "invtdiffvalue": "2000.00",
                "item": "5660",
                "item_display": "S26 PROCAL GOLD 1600 GR",
                "line": "1",
                "location": "103",
                "memo": null,
                "newquantity": "18",
                "quantityonhand": "-2",
                "sys_id": "3017116882588315",
                "sys_parentid": "3017116882548740",
                "unitconversionrate": "1",
                "unitcost": "100.00",
                "units": "1",
                "units_display": "PCS"
            }
        },
So I should change the requestBody.items with requestBody.line?
b
you will need to understand how to access objects that are properties of other objects
m
how to access per line?
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
 define(['N/record'],
 /**
* @param{record} record
*/
 (record) => {

   const post = (requestBody) => {
     // Use create for new records
     var newInvAdj = record.create({
       type: record.Type.INVENTORY_ADJUSTMENT,
       isDynamic: true,
     });

     newInvAdj.setValue('department', requestBody.department);
     newInvAdj.setValue('adjlocation', requestBody.adjlocation);
     newInvAdj.setValue('account', requestBody.account);

     var line1 = {
      'item': requestBody.item,
      'location': requestBody.location,
      'adjustqtyby': requestBody.adjustqtyby
      };

     newInvAdj.selectNewLine({
        sublistId: 'inventory'
        });
     for (var field in line1) {
     newInvAdj.setCurrentSublistValue({
          sublistId: 'inventory',
          fieldId: field,
          value: line1[field]
      });
      }
      newInvAdj.commitLine({
        sublistId: 'inventory'
        });
     newInvAdj.save();
   }

   return { post: post }
 });
When I use that code, I get an error : "name\":\"INVALID_FLD_VALUE\",\"message\":\"Field must contain a value.
But if I change the var line1 value to :
Copy code
var line1 = {
      'item': 3569,
      'location': 1,
      'adjustqtyby': 10
      };
The code is working
This is my json
Copy code
{
  "department": 1,
  "adjlocation": 103,
  "account": 113,
  "customer": 1075,
  "inventory": {
    "line1": {
      "item": 3569,
      "location": 103,
      "adjustqtyby": 5
    }
  }
}
b
same problem as yesterday
go through the examples until you learn how to access a property that itself is an object
108 Views