Friday, December 21, 2018

How to create a sales order with line items using a RESTLet

This article contains sample code for creating a sales order using a RESTLet.  It will dynamically add line items based on the number of line items submitted in the request body.

Sample body package:

{"entity":"123","recordtype":"salesorder","item":[{"item":"10","quantity":"1"},{"item":"11","quantity":"1"}]}

Note: This article requires existing knowledge of programming and RESTLets.

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') // process non-item fields
     {
      record.setFieldValue(fieldname, value);
     }

     if (value && typeof value == 'object') // process line item objects
     {
      for (var itemobject in value)
      {
       record.selectNewLineItem('item');
       var lineitemobject= value[itemobject];
       for (var lineitemfieldname in lineitemobject )
       {          
        var lineitemfieldvalue = lineitemobject[lineitemfieldname];
        record.setCurrentLineItemValue('item',lineitemfieldname,lineitemfieldvalue);      
       }
       record.commitLineItem('item');                            
      }
     }
    }
   }
  }

    var recordId = nlapiSubmitRecord(record);
    return recordId;
}

 

1 comment:

  1. What library should I be using when deploying the script? I am getting when I try it
    "message": "ReferenceError: \"Ext\" is not defined. (app-all.js#1)."

    ReplyDelete