4

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
        };
    });
quarks
  • 33,478
  • 73
  • 290
  • 513
Prabhu
  • 783
  • 2
  • 10
  • 35
  • 1
    Can you share a little more of your Restlet code and some sample JSON that you're sending? Normally you'll want to use `N/format` to get your `Date` object into the appropriate format. – erictgrubaugh Jan 07 '19 at 18:53
  • Added Json, Suitescript 1.0 & 2.0 Restlet code – Prabhu Jan 07 '19 at 19:57

2 Answers2

12

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]));
}
Mike Robbins
  • 3,184
  • 15
  • 20
  • Thanks for the reply. But we are not sure, if custom field will have 'date' in name. – Prabhu Jan 08 '19 at 12:56
  • Update my answer to do field type detection before converting the value to a date. – Mike Robbins Jan 09 '19 at 13:41
  • I have been trying to figure out, how to set a date field value (transaction date) using Map/Reduce Script n Schedule Scripts, finally, I found it. Thanks, Mike – Harish Jun 13 '19 at 23:11
0

I was setting a date field(using Javascript date object as value) in beforeSubmit event of a custom record, and getting the same error in CSV import Invalid date value (must be M/D/YYYY).

After a lot of trial and error, I was able to set date value by adding ignoreFieldChange: true in setValue.

newRecord.setValue({ fieldId: key, value: new Date(), ignoreFieldChange: true });

(Please note that I was getting this error, when I import custom records using CSV Import and set date field in the beforeSubmit UserEvent function.)