We implement NetSuite with world-class passion and professionality, making the experience with the #1 ERP cloud software even more awesome.

Handling data types changes while migrating from SuiteScript 1.0 to 2.x

This is a follow-up on our previous post where we have announced the release of a process for migrating any script from SS1 to SS2 without re-implementing the logic (https://www.ketka.eu/2021/08/03/migrate-from-suitescript-1-0-to-2-x-in-the-blink-of-an-eye/).

One interesting challenge while re-implementing the SS1 API has been that, while in SS1 values read from any field type are returned as strings, in SS2 the returned JS type depends on the type of the field we are reading.

For this reason the functions that return values read from the DB cannot be migrated straight away, but need some data transformation logic.

Let's take as example the following pseudo-code, that represents the re-implementation of nlobjRecord.getFieldValue:

nlobjRecord.prototype.getFieldValue = function(fieldId) {
  // note that this._ss2Record is a private member which
  // is automatically set when a new nlobjRecord is created,
  // e.g. by nlapiLoadRecord or nlapiCreateRecord
  var value = this._ss2Record.getValue({fieldId: fieldId});
  
  if (value is Boolean) {
    // checkbox:
    // - SS2 returns a boolean (true or false)
    // - SS1 returns a string ('T' or 'F')
    value = (value ? 'T' : 'F');
  } else if (value is Number) {
    // integer or decimal:
    // - SS2 returns a native number
    // - SS1 returns a string in computer number format
    value = ''+value;
  } else if (value is Date) {
    // date:
    // - SS2 returns a Date object
    // - SS1 returns a string with formatted date
    value = formatDateToString(value);
  } else if (value is Array) {
    // multiselect:
    // - SS2 returns an Array
    // - SS1 returns a string containing the values joined with the ASCII character 5
    value = value.join(String.fromCharCode(5));
  }
  
  return value;
};

The way of migrating functions that store a value to the DB is simpler, and only the checkbox must be handled in the re-implementation logic, while other data types are already accepted as strings for the SS2 API input parameters.

Looking for example at the re-implementation of nlobjRecord.setFieldValue, this is the corresponding pseudo-code:

nlobjRecord.prototype.setFieldValue = function(fieldId, value) {
  if (value is Boolean) {
    value = (value == 'T');
  }
  
  this._ss2Record.setValue({
    fieldId: fieldId,
    value: value
  });
};
alessio |
September 8, 2021

Leave a Reply

Your email address will not be published. Required fields are marked *