When doing a search on messages for a case. The da...
# suitescript
r
When doing a search on messages for a case. The date value is three hours off from what is displayed on the Communication tab. I know I've seen this before... Why does this happen and how can I fix it? I can think of a couple ways to fix it but I want to make sure I'm doing it right.
message has been deleted
message has been deleted
b
eastern timezone?
the code you have for working with dates only works when your user's timezone matches the server's timezone
server timezone is probably pacific, date/times will be 3 hours off if your user's timezone is eastern
avoid this nonsense by using N/format
in your case, you can probably use
Copy code
loadedCase.setText({fieldId: 'custevent_date_first_contacted', text: minDate});
setText requires a netsuite formatted date/time string, and the value you get from a search result is already in a netsutie date/time format
on a somewhat related different note
your minDate logic may or may not work if your date/time format is different than what you are testing on
i dont know how smart moment is in detecting different formats
r
i think i want the timezone of the user who sent the message... we have users in different timezones
b
you can change your search logic by utilizing sorting order, or minimum summary columns
if you haven't worked with Dates in different timezones before, its best to imagine a Date representing a specific moment in time
there are different strings representing this moment in different timezones, but they are all the same Date
you have 2 choices when setting a Date/Time field. Use setValue with a Date, or setText with a string formatted according to the current users date/time format and timezone preference
for your code, the string you get from
Copy code
result.getValue({name: 'messagedate', join: 'messages'})
is already formatted correctly for the current user
therefore you can directly use it with setText
my warning from before is that your logic for determining minDate might not work for people using different date/time formats
its why i recommended being more clever with the search
you can also use N/format to change the string into a Date, and then do your comparison
r
setText works. so if i'm using new Date() with another field, it will also not default to the user's timezone? can i change this by changing "execute as" on the deployment? sorry, i'm working on an entirely unreasonable deadline.
it works correctly for me with the other fields
see setDateCaseReachedQueue()
b
when you set a date field in netsuite, it has its own representation of the Date
each user sees strings in different formats, but they are still the same Date
new Date represents one moment in time
netsuite will display that Date according to the current user's format
the problem with your code was that you used
Copy code
new Date(minDate)
👍 1
minDate was a string in the current user's timezone
but the Date constructor expects a string in the server's timezone (pacific)
so it constructed a Date that was 3 hours off what you expected it to be
g
Hi @battk @reptar, how could you set a date field using a string? I already used format module but still couldn't do it. For example, I have the string "2020-03-13T145004Z" and I want set a date field with that. Thanks
r
@Gustavo Doná use setText with the text property?
b
convert it to "2020-03-13T145004.000Z" and then make it a Date using
new Date("2020-03-13T14:50:04.000Z")
. Use the resulting Date with setValue.
👍 1