Search code examples
jsonnetsuitesuitescript

NetSuite - Creating PO with line items


Using Restlets I can create any records in NetSuite. However, how do we create records with line items? I know we can use the getLineItemCount, loop through these items and use the setLineItemValue to set the line item.

What I'm not sure about is how we would pass such data to start with. So, we expect an external system to submit some data which I would then need to create POs with line items using my Restlet.

I would ideally like to test this using fire fox Poster, but not sure how to model the data. Something like this works fine to create a normal record using poster by passing data like:

{ "subsidiary" : 2, "entity" : 1084,"currency" : 2,"approvalstatus" : 2}

But how would we send line item data?

My JSon Object looks like this:

{"subsidiary" : 2, 
"entity" : 1275,
"currency" : 2,
"approvalstatus" : 2,
"item": [{"item" : -3, "taxrate": 6},
            {"item" : -3, "taxrate": 6}]
}

I tried getting the data out of the nested jason object with the below code but doesn't quite work...the itemid is blank

for (var x = 1; x <= jsonobject.item.length; x++)
{
    var itemid = record.getLineItemValue('item', jsonobject.item['item'], x);
    nlapiLogExecution('DEBUG', 'itemid', itemid)
    record.setLineItemValue('item', itemid, x);
}

Solution

  • As TonyH mentioned, your code has a bug wherein you should be getting the array index first. In addition, your index should start at 0, not 1, since you are going through a JS array, not a NetSuite sublist:

    for (var x = 0; x < jsonobject.item.length; x++)
    {    
      var itemid = jsonobject.item[x]['item'];
    }
    

    The same would go if you want to get the tax rate:

    for (var x = 0; x < jsonobject.item.length; x++)
    {    
      var taxrate = jsonobject.item[x]['taxrate'];
    }