How to modify business role based on time zone?

shiz
Tera Contributor

I have the following client script code:

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.isNewRecord()) {
     
        //2025-08-04 MOD Start
        //Get current date in UTC
        var now = new Date();
        var utcDay = now.getUTCDay(); // Sunday = 0, Monday = 1, ..., Saturday = 6

        // Calculate the UTC date of Monday of the current week
        var utcMonday = new Date(Date.UTC(
        now.getUTCFullYear(),
        now.getUTCMonth(),
        now.getUTCDate() - (utcDay === 0 ? 6 : utcDay - 1)
        ));
        // Format as YYYY-MM-DD
        var formattedMonday = utcMonday.toISOString().split('T')[0];

        g_form.setValue('u_week_starts_on', formattedMonday);
        //2025-08-04 MOD End
       
        // set field is readonly
        g_form.setReadOnly('u_week_starts_on', false);  
        g_form.setReadOnly('u_assigned_to', false);
}
 
In addition, when checking the Monday settings in the business role, the following code seems to be reporting an error. How to modify the code in the business role to check for non Monday times in any time zone? How can you help me? Thank you so much.

The code for the business role is as follows:
 
(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    var weekStartOn = new GlideDateTime(current.u_week_starts_on);
    var day = weekStartOn.getDayOfWeekLocalTime();
    var day = weekStartOn.getday
    if (day != '1') {
        //Check if the week start on is Monday
        //gs.addErrorMessage('Week start day has been set to Monday in the time sheet policy Default time sheet policy. You must choose a date that is a Monday.'); // Change the message as per your requirement
        gs.addErrorMessage(gs.getMessage('check_week_start_on'));
        current.setAbortAction(true);
    }
})(current, previous);

Thank you very much if you could provide specific modification methods
 
 
6 REPLIES 6

How should the specific business role be written?

t_sadahisa
Giga Guru

Hello @shiz 

 

I tried with PDI and it seems that even if I do setValue in g_form, the value is actually entered in DisplayValue.
Therefore, it should input the value as it is, not in UTC.

 

However, this method depends on the time zone setting of the browser, so I think it is better to set the value according to the user's time zone set in ServiceNow on the server side.