title: NetSuite restlet write performance is poor
body: <p><strong>Edit: I've redacted actual numbers and replaced them with pseudoswears because I've been told sharing performance data is against Netsuite's TOS.</strong></p>
  
  <p>I'm integrating our accounting system with NetSuite using restlets, and overall it has gone pretty well with the glaring exception of performance. I have figured out that nlapiLoadRecord is Satan's own child from a performance perspective, so I avoid it whenever possible favoring the search api and now my read restlets are pretty snappy. However, whenever I ask a restlet to write anything it is as slow as a tortoise stuck in cold tar. I'm generally profiling the nlapiSubmitRecord itself at between <strong>SLOW</strong> and <strong>REALLY DAMN SLOW</strong> seconds. This seems insane to me. No one would use NetSuite if the performance were always this poor for writing. I'll include a couple of code examples below. Any tips on speeding up NetSuite restlet write performance would be appreciated. </p>
  
  <p>In this first one receivedInvoice is the incoming data, and findCurrencyCode and findCustomerByCustomerNumber are well performing functions that do those things. I just clocked this at a nearly unbelievable <strong>HOLY MONKEYS THAT IS SLOW</strong> seconds for a simple invoice with one line item, nearly all of the time passing while I waited for nlapiSubmitRecord to finish.</p>
  
  <pre><code>var createdInvoice = nlapiCreateRecord('invoice');
  
  createdInvoice.setFieldValue('customform', Number(receivedInvoice.transactionType));
  createdInvoice.setFieldValue('memo', receivedInvoice.message);
  createdInvoice.setFieldValue('duedate', receivedInvoice.dateDue);
  
  createdInvoice.setFieldValue('currency', findCurrencyCode(receivedInvoice.currencyUnit));
  createdInvoice.setFieldValue('location', Number(receivedInvoice.location));
  createdInvoice.setFieldValue('postingperiod', findPostingPeriod(receivedInvoice.datePosted));
  var customer = findCustomerByCustomerNumber(receivedInvoice.customerNumber);
  
  createdInvoice.setFieldValue('entity', customer.customerId );
  createdInvoice.setFieldValue('custbody_end_user', customer.customerId );
  createdInvoice.setFieldValue('department', customer.departmentId);
  
  var itemCount = receivedInvoice.items.length;
  for(var i = 0; i &lt; itemCount; i++)
  {
    createdInvoice.selectNewLineItem('item');
    createdInvoice.setCurrentLineItemValue('item', 'item',receivedInvoice.items[i].item);
    createdInvoice.setCurrentLineItemValue('item', 'quantity', receivedInvoice.items[i].quantity);
    createdInvoice.setCurrentLineItemValue('item', 'rate',receivedInvoice.items[i].price);
    createdInvoice.setCurrentLineItemValue('item', 'custcol_list_rate',receivedInvoice.items[i].price);
    createdInvoice.setCurrentLineItemValue('item', 'amount',receivedInvoice.items[i].totalAmount);
    createdInvoice.setCurrentLineItemValue('item', 'description',receivedInvoice.items[i].description);
    createdInvoice.commitLineItem('item');
  }
  
  var recordNumber = nlapiSubmitRecord(createdInvoice,false,true);
  </code></pre>
  
  <p>In this one I think I'm committing a performance heresy by opening the record in dynamic mode, but I'm not sure how else to get the possible line items. Simply opening a new record in dynamic mode clocks in at around <strong>SLOW</strong> seconds. Again, the submit is where most time is being eaten (often around <strong>OH DEAR SWEET MOTHER OF HORRIBLE</strong> seconds), although this one eats a decent amount of time as I mess with the line items, again presumably because I have opened the record in dynamic mode.</p>
  
  <pre><code>var customerPayment = nlapiCreateRecord('customerpayment',{recordmode: 'dynamic'});
  
  customerPayment.setFieldValue('customer', parseInt(customerId));
  customerPayment.setFieldValue('payment', paymentAmount);
  customerPayment.setFieldValue('paymentmethod', paymentMethod);
  customerPayment.setFieldValue('checknum', transactionId);
  customerPayment.setFieldValue('currency', currency);
  customerPayment.setFieldValue('account', account);
  
  var applyCount = customerPayment.getLineItemCount('apply');
  
  if(applyCount&gt;0)
  {
      for(var i=1;i&lt;=applyCount;i++)
      {
          var thisInvoice = customerPayment.getLineItemValue('apply','refnum',i);
  
          if(thisInvoice == invoiceToPay)
          {
              customerPayment.selectLineItem('apply', i);
              customerPayment.setCurrentLineItemValue('apply','apply','T');
              customerPayment.setCurrentLineItemValue('apply', 'amount',paymentAmount);
              customerPayment.commitLineItem('apply');
          }
      }
  
  }
  nlapiSubmitRecord(customerPayment,false,true);
  </code></pre>