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
};
});
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]));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With