Script need to modify asper my client provided the below script fine us but it will trigger when on

Nareshpatel
Tera Expert

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {
    var createdDate = gr.getValue('sys_created_on');
    var createdGlideDate = new GlideDateTime(gr.sys_created_on).getLocalDate();
    var getExpDate = createdGlideDate.getDate();
    var curDateTime = new GlideDateTime();
    var curDate = curDateTime.getLocalDate();
    var forEmailsgetDays = GlideDateTime.subtract(getExpDate, curDate);
    var reminderDays = forEmailsgetDays.getDayPart();
    if (reminderDays > 3 && reminderDays <= 5) {
        // Escalate to line manager's manager
        var lineManager = gr.variables.user_id.manager;
        var managerGR = new GlideRecord('sys_user');
        if (managerGR.get(lineManager)) {
            var managerManager = managerGR.manager;
            if (!managerGR.manager.vip) {
                gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
            }
        }
    }

    else if (reminderDays > 5) {
        // Reject the request
        gr.state = -35;
        gr.stage = 'Closed Rejected';
        gr.approval = 'rejected';
        gr.active = 'false';
        gr.comments = "Request rejected due to lack of approval within 5 days";
        gr.setWorkflow(false);
        gr.update();



    }

Please provide additional script like it will trigger email to manager only working days irrespective all days 

6 ACCEPTED SOLUTIONS

Ehab Pilloor
Mega Sage

Hi @Nareshpatel,

 

Your script has issues with subtraction. You can try this updated script.

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();

while (gr.next()) {
    var createdGDT = gr.getValue('sys_created_on');
    var created = new GlideDateTime(createdGDT);
    var now = new GlideDateTime();

    var diff = GlideDateTime.subtract(now, created);
    var diffDays = Math.floor(diff.getNumericValue() / (1000 * 60 * 60 * 24)); 

    if (diffDays > 3 && diffDays <= 5) {
        // Escalate to line manager's manager
        var lineManager = gr.variables.user_id.manager;
        var managerGR = new GlideRecord('sys_user');
        if (managerGR.get(lineManager)) {
            var managerManager = managerGR.manager;
            if (managerManager && !managerManager.vip) {
                gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
            }
        }
    } 
    else if (diffDays > 5) {
        // Reject the request
        gr.state = -35;
        gr.stage = 'Closed Rejected';
        gr.approval = 'rejected';
        gr.active = 'false';
        gr.comments = "Request rejected due to lack of approval within 5 days";
        gr.setWorkflow(false);
        gr.update();
    }
}

 

Regards,

Ehab Pilloor

View solution in original post

Shraddha Kadam
Mega Sage

Hello @Nareshpatel ,

 

Please use code below so it will trigger email to manager only working days irrespective all days -

            // Define your business calendar sys_id.
            // IMPORTANT: Replace 'YOUR_BUSINESS_CALENDAR_SYS_ID' with the actual Sys ID
            // of your organization's working days calendar (e.g., '8-5 weekdays').
            // You can find this in System Scheduler > Schedules > Schedules.
            var businessCalendarSysId = 'YOUR_BUSINESS_CALENDAR_SYS_ID';
            var schedule = new GlideSchedule(businessCalendarSysId);
            var currentTime = new GlideDateTime(); // Get the current date and time

            // Check if the current time falls within the defined schedule (working hours and days)
            if (schedule.isInSchedule(currentTime)) {
                // Only send email if it's a working day AND not a VIP manager
                if (!managerGR.manager.vip) {
                    gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
                    gs.log('Email sent for RITM: ' + gr.number + ' to line manager\'s manager. Reminder days: ' + reminderDays);
                } else {
                    gs.log('Email not sent for RITM: ' + gr.number + '. Manager is VIP.');
                }
            } else {
                gs.log('Email not sent for RITM: ' + gr.number + '. Today (' + currentTime.getDisplayValueInternal() + ') is not a working day based on calendar: ' + businessCalendarSysId);
            }
If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Nareshpatel 

what's your actual business requirement?

what did you configure and where have you written the above script?

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

View solution in original post

Can you provide latest script ?

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

@Nareshpatel ,

I want to confirm the requirement again, so you want to send the notification for all days?

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

Yes, I am checking

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

23 REPLIES 23

Hello @Nareshpatel ,

 

This looks good.

If my response was helpful, please mark it as correct and helpful.
Thank you.

if i created the one RITM based on the above script i  need to wait till tomorrow  Friday 5 whether triggered mail or not ? i' m getting little bit confuse here pls give suggestion? or

Hello @Nareshpatel ,

 

No, email will not trigger because you mentioned answer = false. The email will trigger only from Sunday to Thursday.

If my response was helpful, please mark it as correct and helpful.
Thank you.

Hi @Nareshpatel

 

For debugging purpose, you can use GlideDateTime API with date inside it having present and different days of the week and pass in your script to test and see if it works in those different scenarios or not. This will give an idea which day the script will work and which day it will not.

var rightNow = new GlideDateTime('ADD_DIFFERENT_DATES_OF_A_WEEK_HERE_TO_TEST');
//var dayOfWeek = rightNow.getDayOfWeekUTC();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
           //6=Saturday, 5=Friday
if (dayOfWeek == 5 || dayOfWeek == 6) {
    answer = false;
} else {
    answer = true;

Regards,

Ehab Pilloor

Shraddha Kadam
Mega Sage

Hello @Nareshpatel ,

 

Please use code below so it will trigger email to manager only working days irrespective all days -

            // Define your business calendar sys_id.
            // IMPORTANT: Replace 'YOUR_BUSINESS_CALENDAR_SYS_ID' with the actual Sys ID
            // of your organization's working days calendar (e.g., '8-5 weekdays').
            // You can find this in System Scheduler > Schedules > Schedules.
            var businessCalendarSysId = 'YOUR_BUSINESS_CALENDAR_SYS_ID';
            var schedule = new GlideSchedule(businessCalendarSysId);
            var currentTime = new GlideDateTime(); // Get the current date and time

            // Check if the current time falls within the defined schedule (working hours and days)
            if (schedule.isInSchedule(currentTime)) {
                // Only send email if it's a working day AND not a VIP manager
                if (!managerGR.manager.vip) {
                    gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
                    gs.log('Email sent for RITM: ' + gr.number + ' to line manager\'s manager. Reminder days: ' + reminderDays);
                } else {
                    gs.log('Email not sent for RITM: ' + gr.number + '. Manager is VIP.');
                }
            } else {
                gs.log('Email not sent for RITM: ' + gr.number + '. Today (' + currentTime.getDisplayValueInternal() + ') is not a working day based on calendar: ' + businessCalendarSysId);
            }
If my response was helpful, please mark it as correct and helpful.
Thank you.