Monday, July 15, 2019

Set Date and Time value of a custom field based on User's preferred Time Zone

To set the date and time value of a custom field based on User's preferred Time Zone, we can use the sample script below.
Script below captures the current date and time when the Sales Order record is created based on the User's preferred Time Zone.

For the sample, the User's preferred Time Zone is 'Asia/Manila' or (GMT+08:00) Manila.

function beforeSubmit(type) {
 nlapiLogExecution('DEBUG', 'Before Submit', 'type = ' + type);

 var d = new Date();
 var yyyy = d.getFullYear().toString();
 var mm = (d.getMonth() + 1).toString();
 var dd = d.getDate().toString();
 var time = formatAMPM(d);
 var val = (mm[1] ? mm : mm[0]) + '/' + (dd[1] ? dd : dd[0]) + '/' + yyyy + " " + time;

 var conf = nlapiLoadConfiguration('userpreferences');
 var tz = conf.getFieldValue('TIMEZONE');

 // The current date and time captured within the server is set to PST that is why we need to set it first to "America/Los_Angeles"
 // "custbody_orig_date" is the internal id of the custom field with field type of "Date/Time"
 
 nlapiSetDateTimeValue('custbody_orig_date', val, 'America/Los_Angeles');

 // We then set the Time Zone to the User's preferred
 nlapiSetDateTimeValue('custbody_orig_date', nlapiGetDateTimeValue('custbody_orig_date'), tz);
}

No comments:

Post a Comment