This topic contains 4 replies, has 0 voices, and was last updated by Nic 8 years, 6 months ago.

  • Author
    Posts
  • #6706

    Nic

    Below is the RESTlet, the sample one given by NetSuite. I test the the RESTlet with a customer and it works. I am trying to modify so it will create an invoice and therefore needs to add a line item as well. I am trying to get this to work but as of now it says that the item is an invalid reference key, which it is valid

    I think I need to work on parsing out the nested JSON array.

    Sorry I just keep on working on this and trying to update this as I go along.

    HTML Code:
    if (value && typeof value != ‘object’) // ignore other type of parameters
    {
    record.setFieldValue(fieldname, value);
    }
    else {
    record.setLineItemValue(‘item’, fieldname, 1, value) // insert the line item in the RESTlet
    }

    SAMPLE FROM NETSUITE

    HTML Code:
    // Create a standard NetSuite record
    function createRecord(datain)
    {
    var err = new Object();

    // Validate if mandatory record type is set in the request
    if (!datain.recordtype)
    {
    err.status = “failed”;
    err.message= “missing recordtype”;
    return err;
    }

    var record = nlapiCreateRecord(datain.recordtype);

    for (var fieldname in datain)
    {
    if (datain.hasOwnProperty(fieldname))
    {
    if (fieldname != ‘recordtype’ && fieldname != ‘id’)
    {
    var value = datain[fieldname];
    if (value && typeof value != ‘object’) // ignore other type of parameters
    {
    record.setFieldValue(fieldname, value);
    }
    }
    }
    }
    var recordId = nlapiSubmitRecord(record);
    nlapiLogExecution(‘DEBUG’,’id=’+recordId);

    var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
    return nlobj;
    }
    Sample POST to RESTlet using cURL

    HTML Code:
    curl -X POST
    -H “Content-Type: application/json”
    -H “Authorization: NLAuth nlauth_account=xxxx,nlauth_email=xxxxxxxxx,nlauth_ signature=xxxxxx, nlauth_role=3”
    -d
    ‘{
    “recordtype”:”invoice”,
    “total”:64.04,
    “subsidiary “:1,
    “tranid”:”120″,
    “entity”:”271680″,
    “salesrep”:”1 26605″,
    “terms”:”4″,
    “custbody_bcg_contract_book”:”276426″,
    “item”:
    [{“amount”:8.96,”rate”:8.96,”item”:726,”quantity “:1,”istaxable”:false}]
    }’
    “https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl?script=xxx&deploy=x”
    This is a cached copy. Click here to see the original post.

  • #6707

    Daniel Giguere

    Originally posted by Nic

    View Post

    I think I need to work on parsing out the nested JSON array.

    Correct. Line items are not processed due to this line:

    Originally posted by Nic

    View Post

    if (value && typeof value != ‘object’) // ignore other type of parameters

    typeof item evaluates to ‘object’. You could add a else statement to process line items.

  • #6708

    Nic

    Daniel, thanks for confirming my suspicion. I tried using an else statement, I am getting an error that it is not a correct item number for the subsidiary. But I am sure it is an item for that subsidiary

    HTML Code:
    if (value && typeof value != ‘object’){ // ignore other type of parameters
    record.setFieldValue(fieldname, value);
    }
    else {
    record.setLineItemValue(‘item’, fieldname, 1, value) // insert the line item in the RESTlet
    }

  • #6709

    Daniel Giguere

    Hi Nic,

    Based on the sample code you mentioned in your original post, it would look like that:

    Code:
    for (var fieldname in datain) {
    if (datain.hasOwnProperty(fieldname)) {
    if (fieldname != ‘recordtype’ && fieldname != ‘id’) {
    var value = datain[fieldname];
    if (value && typeof value != ‘object’) { // ignore other type of parameters
    record.setFieldValue(fieldname, value);
    } else if (value && fieldname == ‘item’) {

    // Loop through line items
    var items = value;
    for (var i = 0; i < items.length; i++) {
    var currentItem = items[i];
    record.selectNewLineItem('item');
    record.setCurrentLineItemValue('item', 'item', currentItem["item"]);
    record.setCurrentLineItemValue('item', 'quantity', currentItem["quantity"]);
    record.setCurrentLineItemValue('item', 'rate', currentItem["rate"]);
    record.commitLineItem('item');
    }

    }
    }
    }
    }

  • #6710

    Nic

    Thank you so much!!!

You must be logged in to reply to this topic.