My experience is that server side SS2 demands a da...
# suitescript
j
My experience is that server side SS2 demands a date object that was created using a string formatted with the user/account's date format preference. E.g. if the account date format preference is MM/DD/YYYY, you need a do
new Date(datestring)
where datestring is formatted MM/DD/YYYY.
s
that sounds broken, unless ES5.1 leaves
datestring
as a implementation defined format?
that is, I'd expect
new Date()
to accept whatever values any ES5.1 environment would.
j
IIRC error happens when you pass the date to
rec.setValue
or press rec.save() Based on the error messages I remember, the Rhino backend would create different java classes for Dates created from different string formats, and the internal NS API expected a specific java class.
s
that's horrible
I haven't seen this behavior yet 🤞
j
I just tried and could not reproduce the bug, but I did find one of the errors logged for it this was from middle of last year
Copy code
You have entered an Invalid Field Value org.mozilla.javascript.NativeDate@5f0736f for the following field: custbody_tpm_ship_by
b
This is exactly the error I faced .
j
You might be able to get around it by fiddling with
N/format
which is supposed to respect the user/account preference
Copy code
var origFmtString = '2019-03-15T14:42:52.022Z';
var origFmtDate = new Date(origFmtString);
var accountFmtString = format.format({ type: format.Type.DATE, value: origFmtDate });
An approach I've used was to explicitly get the user's date format and use moment
Copy code
function datestr(d) {
	var fmt = runtime.getCurrentUser().getPreference({
		name: 'DATEFORMAT'
	});
	var m = moment.utc(d);
	return m.isValid() ? m.format(fmt) : '';
}
c
This is horrific. Everyone has a different answer/experience. Maybe there's a definitive SuiteAnswer...
j
I don't disagree. It's pretty bad. One would have hoped that the idea behind passing a Date object (in SS1 you passed a string) to the NS API was so NS can decode a timestamp and not be concerned with format of input string.