I have a sql returning 02/05/2024 8:40 am. When I ...
# suitescript
n
I have a sql returning 02/05/2024 8:40 am. When I format that and then try and set my DateTime field like the following it sets the time to 11:40 am.
Copy code
var nyTime = format.format({
        value:date,
        type:format.Type.DATETIME,
        timezone:format.Timezone.AMERICA_NEWYORK
    });

record.submitFields({
                        type: 'customrecord_dc_wo',
                        id: wo,
                        values: {
                            custrecord_dc_wo_eq_lot_check_created: new Date(nyTime)
                        }
})
👀 1
e
What type of script is this?
n
MapReduce
a
are you in another time zone? because that can affect how it comes out
n
Im EST time zone
e
I believe the timezone of any user-less server-side script is Pacific. So my guess is that
nyTime
outputs 08:40 as a string, then when you pass that through
new Date()
it assumes Pacific
then when you view the date in the UI with your Eastern preference, you see 11:40
n
yes correct
So, do I need to format as Pacific before the
new Date()
e
Do you need
date
as a string at all? If you just leave it as a
Date
instance (i.e. pass
date
as the value for the field), does this go away?
n
if you just set recordSubmit with date instead of
new Date(date)
you get an error:
Copy code
ERROR String: {"type":"error.SuiteScriptError","name":"INVALID_FLD_VALUE","message":"You have entered an Invalid Field Value 02/08/2024 08:40 am for the following field: custrecord_dc_wo_eq_lot_check_created"
e
I was expecting
date
to be a
Date
instance
n
I tried new Date(date) as well and it still sets 11:40
e
Yeah, your
timezone
s/b Pacific iso New York
I thought
date
was already a
Date
, so I wasn't sure about the need for
nyTime
as a string
Depending on where
date
is coming from, you probably don't need
format.format
at all
n
So, just out of the SQL it outputs 8:40am then I do new Date(date). Are you suggesting getting pacific time from the sql first then using that in the Date instance? Because format utilizing pacific doesn't set right.
a
if you are just using format.format to change the timezone, you can do that using the Date object and then use it to set the field. I think that is what he is saying.
e
Copy code
// Assuming the string:
date = "02/05/2024 8:40 am"

// I'm suggesting
record.submitFields({
    type: 'customrecord_dc_wo',
    id: wo,
    values: {
      custrecord_dc_wo_eq_lot_check_created: new Date(date)
   }
})
The
Date
constructor will assume Pacific, and the SQL should be returning Pacific, so I'm not sure there's a need to do any conversion
b
there are 2 timezones of importance here
first the timezone of the application server that is running your code
that is pacific timezone
n
So, here is the SQL output and the custom record created date. I was running just like your code there @erictgrubaugh and the field was setting as 11:40am instead
b
the second is the timezone of your netsuite user
which so far seems to be Eastern timezone, though you probably want to check your company timezone preferences
or in rare cases, your primary subsidiarie's timezone preferences
😄 1
the string you get from query is '02/05/2024 8:40 am', it doesnt have timezone information in it
but it will be in the timezone of your user
which hopefully is eastern according to your description so far
n
yes, company and user is EST
b
you then do
Copy code
format.format({
        value:date,
        type:format.Type.DATETIME,
        timezone:format.Timezone.AMERICA_NEWYORK
    });
which is actually irrelevant
format.format turns a date into a string
and date isnt actually a Date, its a string
format.format has terrible error handling behavior
it will just return the unmodified input
so it does nothing and doesnt throw an error
so now nyTime is the string
02/05/2024 8:40 am
you then do
Copy code
new Date(nyTime)
keeping in mind that the application server is running in pacific time, you passed the date constructor a string with no timezone information
so it assumes its pacific
and now your date is destroyed, and is now 02/05/2024 8:40 am pacific time, where before it was 02/05/2024 8:40 am eastern time
you pass 02/05/2024 8:40 am pacific time to the record.submitFields, and since your user is using eastern time, it converts the pacific time to eastern, so you now have 02/05/2024 11:40 am
n
Right, so doing the following sets the field at 11:40 am.
Copy code
//DATE FROM SQL
var date = '02/05/2024 8:40 am';

var formattedDate = format.format({
    value: date,
    type: format.Type.DATETIME,
    timezone: format.Timezone.AMERICA_NEWYORK
});

record.submitFields({
    type: 'customrecord_dc_wo',
    id: wo,
    values: {
        custrecord_dc_wo_eq_lot_check_created: new Date(formattedDate)
    }
})
Are you saying i need to get the pacific time instead of EST time as a string from the SQL before doing formatting?
b
format.format can turn a date into a string
which is not helping since you dont have a date
instead, you want to turn a string into a Date, which is the purpose of format.parse
if you had a Date, you also wouldnt need new Date(formattedDate), you would already have your Date
n
ahhh yes I see what you mean, thanks @battk format.parse worked perfect
e
So the query is returning Eastern time because the User in this context is the System user, and the System user uses the Company time zone, which is Eastern?
n
Yes, user (me) is set as eastern and under company information its Eastern
e
You're not the User in a M/R though, so your timezone doesn't come into play until display time in the UI. I was expecting the query to return Server time rather than Company time, so that's what I'm trying to clear up.
n
ohh sorry. yeah, the sql was returning eastern from what I can tell and not pacific/server time
b
a scheduled deployment of a map/reduce script will run using the system user (same for a script executed via the save and execute button)
a deployment run using N/task will be run using the user of whoever ran the script
👀 1
which is often a user other than system when its used by user events, suitelets, or restlets
netsuite will usually format dates using the timezone format of the user
which will match the user if its a real user
or will be the default timezone for the system user, which is actually the timezone preference of the primary subsidiary
in the vast majority of circumstances, thats the same as the timezone on the company preferences
n
company, user, and subsidiaries are all in Eastern, so was confused when it set as pacific. Thanks for your help
e
Thanks, that helps.
t
I had specifically had issues with this because I needed to run saved search date queries...
b
that note isnt specific enough, but its referring to the behavior of Date formatting, which is not the same as DateTime formatting
w
If you can have the sql return the iso timestamp shouldn't you be able to just run new Date() on it:
to_char(sys_extract_utc(systimestamp), 'yyyy-mm-dd"T"hh24:mi:ss"Z"')
d
@Watz, I've used this approach before with success. I wonder if a blog with some sort of event/script matrix would help here.
👀 2