1
votes

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);
}
2
You've got a bug I think. var itemid = record.getLineItemValue('item', jsonobject.item['item'], x); should be var itemid = record.getLineItemValue('item', jsonobject.item[x]['item'], x); - TonyH
I tired your suggestion but it throws an error - for exmaple if I try to get back the taxcode: TypeError: Cannot read property "taxcode" from undefined. - MG2016

2 Answers

1
votes

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'];
}
3
votes

You could try using an array within your JSON to encapsulate the line items such as:

{"subsidiary" : 2, 
"entity" : 1084,
"currency" : 2,
"approvalstatus" : 2,
"items": [{name:"item1", price: "100"},
        {name:"item2", price:"200"}]
}

Your RESTlet code would have to then digest this, and call the relevant NS functions you mention.