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

pls could you update the script using my script

Yes, I am checking

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

Hello @Nareshpatel ,

 

Please use below code and see if that is working -

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();
    gs.info("Your current day is - " +curDateTime.getDayOfWeek());
    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) {
				//Monday = 7, Friday = 4 
				// if your current day is betwwen Monday to Friday then only notification will trigger 
                if (curDateTime.getDayOfWeek() == 7 || curDateTime.getDayOfWeek() == 1 || curDateTime.getDayOfWeek() == 2 || curDateTime.getDayOfWeek() == 3 || curDateTime.getDayOfWeek() == 4) {
                    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();
    }
}

 

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

Shraddha Kadam
Mega Sage

Hello @Nareshpatel ,

I agree with @Ehab Pilloor . Once you have the final solution, you can accept the solution.

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