Monday, April 1, 2019

Get logged in user's current time in server side SuiteScript

When calling new Date() in a server side script such as a user event script, scheduled script or suitelet the time is returned in PST (GMT -07:00). To get the time of the current logged in user, follow these steps:

1. Create a custom record type by going to Customization > Lists, Records, and Fields > Record Types > New
-Name the record CurrentTime
-Set ID as customrecord_currenttime
-Uncheck the Include Name checkbox
-Save

2. Create a new field
-Label the field Current Time Now
-Set ID as custrecord_currenttime_now
-Set Type to Date/Time
-Uncheck the Store Value checkbox
-In the Validation & Defaulting tab set Dynamic Default to be Current Date/Time
-Save

3. Create a new custom record by going to the record type from step 1 and clicking on View Records in the cross links. Then click New.
-Save

4. In any script where the user's current time is required use this code:
var usersTime = nlapiLoadRecord('customrecord_currenttime', 1).getDateTimeValue('custrecord_currenttime_now', nlapiLoadConfiguration("userpreferences").getFieldValue("TIMEZONE"));

No comments:

Post a Comment