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

Ankur Bawiskar
Tera Patron
Tera Patron

@shiz 

what's your actual business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Using the following business role, the current time zone is Tokyo, and obtaining the week start on Monday is not a problem. But when I change the time zone of snow to us, due to the time difference, the 'day' obtained by the business role below is not equal to '1', which will result in an error. So I would like to ask if it is necessary to modify the code logic of the business role. How should I modify it?

 

(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);

@shiz 

what's your business requirement?

You are sharing the issue faced in the technical implementation

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Omkar Kumbhar
Mega Sage
Mega Sage

@shiz remove the quotes for day! = 1

 

 

function executeRule(current, previous /*null when async*/) {

var weekStartOn = new GlideDateTime(current.u_week_starts_on);
var day = weekStartOn.getDayOfWeekLocalTime();

// Check if the selected day is NOT Monday (Monday = 1)
if (day != 1) {
gs.addErrorMessage(gs.getMessage('check_week_start_on'));
current.setAbortAction(true);
}
}

 

Thank you.

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.