Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set date for RESTlet in netsuite with Suitescript 2.0

Tags:

netsuite

I am getting error while creating phonecall activity through RESTlet, even I send correct format.

Invalid date value (must be M/D/YYYY)

It works fine in Suitescript 1.0. Phonecall has many standard date fields & can have custom date fields too.

If need to convert those date fields to acceptable format in Restlet, need to identify all date & time type fields.

Is there any other way how to proceed that?

JSON

{
    "title":"test",
    "startdate":"01/08/2019",
    "resourceType":"phonecall"
}

It works fine in suitescript 1.0

function post(datain) {
          var record = nlapiCreateRecord(datain.resourceType);
          for (var fieldname in datain) {
              if (datain.hasOwnProperty(fieldname)) {
                  if (fieldname != 'resourceType' && fieldname != 'id') {
                      var value = datain[fieldname];
                      record.setFieldValue(fieldname, value);
                  }
              }
          }
          var recordId = nlapiSubmitRecord(record);
          nlapiLogExecution('DEBUG', 'id=' + recordId);
          var nlobj = nlapiLoadRecord(datain.resourceType, recordId);
          return nlobj;
}

Not working in Suitescript 2.0

/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */

define(['N/record'],function(record) {
        function post(context) {
            var resourceType = context.resourceType
            delete context.resourceType
            var objectRecord = record.create({
                type: resourceType
            });

            for (var fldName in context) {
                if (context.hasOwnProperty(fldName)) {
                    objectRecord.setValue(fldName, context[fldName]);
                }
            }
            var createdId = objectRecord.save({});
            return getById(resourceType, createdId);
        }

        function getById(resourceType, recordId) {
            if (recordId != undefined) {
                var response = record.load({
                    type: resourceType,
                    id: recordId
                });

                return response;
            }
        }
        return {
          post: post
        };
    });
like image 985
Prabhu Avatar asked Dec 24 '22 01:12

Prabhu


1 Answers

setValue() for a date field in SS2.0 wants a JavaScript date object for the value rather than a string. So you can do this:

for (var fldName in context) {
  if (context.hasOwnProperty(fldName)) {
    if (fldName === 'startdate') {
      objectRecord.setValue(fldname, new Date(context[fldName]));
    }
    objectRecord.setValue(fldName, context[fldName]);
  }
}

It looks like you're trying to create a generic record update API so maybe check to see if the field name 'contains' date and then convert it.

if (fldName.indexOf('date') >= 0) {
  objectRecord.setValue(fldname, new Date(context[fldName]));
}

EDIT: You could do some field type detection before setting the value:

var field = objectRecord.getField{ fieldId: fldName });
if (field.type === 'date') {
  objectRecord.setValue(fldName, new Date(context[fldName]));
}
like image 164
Mike Robbins Avatar answered Jan 25 '23 18:01

Mike Robbins