Monday, June 17, 2019

Restlet > Create Invoice

Please refer to the Restlet code below which uses the Data passed via POST request :

- Customization > Scripting > Scripts > New : Restlet > function POST

function setRecord(data)
{
 var retObj = createInvoice(data);

 return JSON.stringify(retObj);
}

function createInvoice(data)
{
 var record = nlapiCreateRecord('invoice');
 
 var properties = {
  'location': '',
  'taxItem': '',
  'itemList': ''
 };
 
 record.setFieldValue('customform', '116');
 record.setFieldValue('entity', '8');
 
 for (p in properties)
 {
  if (typeof data[p] == 'undefined')
  {
   return { 'error': 'Missing required field: ' + p };
  }
  
  var value = data[p];
  
  if (p == 'itemList' && typeof value == 'object')
  {
   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');                            
   }
  }
  else
  {
   record.setFieldValue(p, value);
  }
 }

 var recordId = nlapiSubmitRecord(record);
 return { internalId: recordId };
}

Restlet POST request sample (Also add the authorization and content-type headers):

- {"entity":"8","recordtype":"invoice","location":"1","taxItem":"CA-ALAMEDA","itemList":[{"item":"4","quantity":"1"}]}


*This is for SuiteScript version 1.0

1 comment:

  1. Hi Could you please share sample code for INVOICE creation

    ReplyDelete