I received this error " ```Invalid date value (mus...
# general
a
I received this error "
Copy code
Invalid date value (must be D/M/YYYY)
" with RESTlet script, I am using POST method in postman to create a record via suitescript 2.0 and I have to set value for date field and the type of field is Date and I faced this issue, I was searching and trying multiple solution but none of all was succeeded. Anyone can help me ?!
c
Not enough information. Also, the message is quite clear. Share your code. Best to post in #suitescript
a
// Create a NetSuite record from request params     function _post(context) {         doValidation([context.recordtype], ['recordtype'], 'POST');         var rec = record.create({             type: context.recordtype,             isDynamic: true,         });         for (var fldName in context)             if (context.hasOwnProperty(fldName))                 if (fldName !== 'recordtype') {                     if (fldName === 'custrecord_az_payroll_sheet_date') {                         rec.setValue(fldName, context[fldName])                     }                     rec.setValue(fldName, context[fldName])                 }         var submitRec = rec.save({             enableSourcing: true,             ignoreMandatoryFields: true         });         var output = new Object();         output.submitRec = submitRec;         return output;     }
above this is my code
c
Oh, @battk will enjoy this
k
Can you share the payload that errored out?
b
wont really matter, the code given cant set date fields
use setValue with a Date to set a date or date/time field
alternatively use setText with a string to set a date or date/time field
k
Really? I use setValue() for setting date all the time... it just needs to be in the right format
b
the code given has values taken directly from the JSON from the request, JSON has no representation for a Date
💯 1
k
While JSON doesn't specify how dates are represented, UTC is widely used... but in this case a string could've been sent... did I miss something?
b
a string can't be used with setValue to set a date/time field, in fact no value that JSON supports can do so
its why the actual json from the request doesnt matter, the code for the RESTlet needs to handle date fields differently
1
a
@battk I try setValue with date object and same issue // Create a NetSuite record from request params     function _post(context) {         doValidation([context.recordtype], ['recordtype'], 'POST');         var rec = record.create({             type: context.recordtype,             isDynamic: true,         });         for (var fldName in context)             if (context.hasOwnProperty(fldName))                 if (fldName !== 'recordtype') {                     if (fldName === 'custrecord_az_payroll_sheet_date') {                         rec.setValue(fldName, new Date(context[fldName]))                     }                     rec.setValue(fldName, context[fldName])                 }         var submitRec = rec.save({             enableSourcing: true,             ignoreMandatoryFields: true         });         var output = new Object();         output.submitRec = submitRec;         return output;     }
c
The 2nd setValue executes unconditionally
a
@CD yes I get the issue, I used the below code // Create a NetSuite record from request params     function _post(context) {         doValidation([context.recordtype], ['recordtype'], 'POST');         var rec = record.create({             type: context.recordtype,             isDynamic: true,         });         for (var fldName in context)             if (context.hasOwnProperty(fldName))                 if (fldName !== 'recordtype') {                     var fldType = rec.getField({                         fieldId: fldName                     })                     if (fldType.type === 'date') {                         rec.setValue(fldName, new Date(context[fldName]))                     }                     else {                         rec.setValue(fldName, context[fldName])                     }                 }         var submitRec = rec.save({             enableSourcing: true,             ignoreMandatoryFields: true         });         var output = new Object();         output.submitRec = submitRec;         return output;     }
but if in JSON write "9/11/2021" it created record with "11/9/2021" it replace month and day position
b
go over how the Date constructor works, you are using the least safe method to create a Date
i dont think the Date constructor is causing your particular issue, its more likely that you need to understand how Date Formats work in NetSuite.
a
Thanks all my friends, I did it
156 Views