How to find the date difference via client script in agent workspace

roshini1
Kilo Guru

I am having two fields start date and end date in my form and am trying to check whether the end date is less than start date, if yes it should throw and error. The script that i wrote is working in native view, but not in agent workspace.
Can someone help me out

var start_date = g_form.getValue("u_outage_start_date");
var end_date = g_form.getValue("u_outage_end_date");
var format = g_user_date_time_format;
var isEndBeforeStart = compareDates(start_date, format, end_date, format);
if (isEndBeforeStart){
g_form.clearValue("u_outage_end_date");
g_form.showFieldMsg("u_outage_end_date", "Outage End Date should be after Outage Start Date", "error");
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

g_user_date_time_format seems not allowed in workspace

you can use GlideAjax

Regards
Ankur

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

View solution in original post

10 REPLIES 10

IvanBarsukov
Tera Expert

I had the same issue in the Configurable Workspace and I was able to monkey-patch it the following way:

 

- onLoad Task Client Script (Inherited=true):

function onLoad() {
    try {
        if (typeof g_user_date_format === 'undefined' || !g_user_date_format) {
            g_user_date_format = ux_globals.session.output.user.dateFormat;
        }

        if (typeof g_user_date_time_format === 'undefined' || !g_user_date_time_format) {
            g_user_date_time_format = ux_globals.session.output.user.dateTimeFormat;
        }

        if (typeof g_first_day_of_week === 'undefined' && typeof g_scratchpad.g_first_day_of_week !== 'undefined') {
            g_first_day_of_week = g_scratchpad.g_first_day_of_week;
        }
    } catch (e) {
        console.error('Fixing the global variables for the Workspace failed', e);
    }
}

- onDisplay Task Business Rule:

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

    g_scratchpad.g_first_day_of_week = parseInt(gs.getProperty('glide.ui.date_format.first_day_of_week', 1));

})(current, previous);

 
The Client Script was mainly inspired by this script (see the evaluateTermDate function):

https://<instance>.service-now.com/scripts/sn/common/clientScript/uiPolicyFactory.js