Scheduled Job Condition Script Not working when Execute Now

Gpope
Tera Contributor

Hi All! 

 

I'm working in a scheduled job to trigger a subflow to create bulk cases. Stakeholder want to create this cases on specific days, dates. (7th day of the month - 5th day of month ) . 

 

I've created a scripted condition like below to trigger the subflow on 1st day of each month : ( on scripts - background works correctly ) 

 

answer = checkdate();
gs.info(answer);

function checkdate() {
	var answer = false;

    var today = new GlideDateTime('2024-03-01 18:38:34');
    gs.info(' RG:: Today is: ' + today);

    var endofMonth = new GlideDateTime(today);
    endofMonth.setDayOfMonthLocalTime(1);
    gs.info(' RG:: FIRST DAY of month is: ' + endofMonth);

    var gdt = new GlideDateTime(endofMonth);
    gdt.getDayOfWeekLocalTime()
    gs.info(' RG:: Day of Week: ' + gdt.getDayOfWeekLocalTime());

    if (gdt.getDayOfWeekLocalTime() == "6") {
        var l = new GlideDateTime(endofMonth);
        l.addDaysLocalTime(2);
        gs.info('RG::' + l);
        var dura = new GlideDuration();
        dura = GlideDateTime.subtract(l, today);
        var i = dura.getRoundedDayPart();
        gs.info('RG::' + i);
        if (i == 0) {
            gs.info(' RG:: l');
            answer = true;
        }
		// else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "7") {
        var k = new GlideDateTime(endofMonth);
        k.addDaysLocalTime(1);
        gs.info('RG::' + k);
        var durat = new GlideDuration();
        durat = GlideDateTime.subtract(k, today);
        var o = dur.getRoundedDayPart();
        gs.info('RG::' + o);
        if (o == 0) {
            gs.info('RG:: k');
             answer = true;
        } 
		//else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "1" || gdt.getDayOfWeekLocalTime() == "2" || gdt.getDayOfWeekLocalTime() == "3" || gdt.getDayOfWeekLocalTime() == "4" || gdt.getDayOfWeekLocalTime() == "5") {

        var h = new GlideDateTime(endofMonth);
        gs.info('RG:: is day of the week ' + h);

        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(h, today);
        var u = dur.getRoundedDayPart();
        gs.info('RG :SUBTRACT: ' + u);

        if (u == 0) {
            gs.info('RG: is equal to 0  : h');
            answer = true;
        } 
		// else {
        //     return false;
        // }
    } else {
		gs.info('RG:: false');
         answer = false;
    }
return answer;

}

 

I know that the script to run the subflow works, because when I execute it without condition WORKS. 

 

Do you know what can happen in this case ? What Im missing? 

 

Thanks!! 

1 ACCEPTED SOLUTION

Hey @Gpope,

 

No worries, it works in my PDI, the condition is:

var answer = checkdate();
answer;
function checkdate() {
    var answer = false;
    var today = new GlideDateTime('2024-03-01 18:38:34');
    gs.info(' RG:: Today is: ' + today);

    var endofMonth = new GlideDateTime(today);
    endofMonth.setDayOfMonthLocalTime(1);
    gs.info(' RG:: FIRST DAY of month is: ' + endofMonth);

    var gdt = new GlideDateTime(endofMonth);
    gdt.getDayOfWeekLocalTime()
    gs.info(' RG:: Day of Week: ' + gdt.getDayOfWeekLocalTime());

    if (gdt.getDayOfWeekLocalTime() == "6") {
        var l = new GlideDateTime(endofMonth);
        l.addDaysLocalTime(2);
        gs.info('RG::' + l);
        var dura = new GlideDuration();
        dura = GlideDateTime.subtract(l, today);
        var i = dura.getRoundedDayPart();
        gs.info('RG::' + i);
        if (i == 0) {
            gs.info(' RG:: l');
            answer = true;
        }
        // else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "7") {
        var k = new GlideDateTime(endofMonth);
        k.addDaysLocalTime(1);
        gs.info('RG::' + k);
        var durat = new GlideDuration();
        durat = GlideDateTime.subtract(k, today);
        var o = dur.getRoundedDayPart();
        gs.info('RG::' + o);
        if (o == 0) {
            gs.info('RG:: k');
            answer = true;
        }
        //else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "1" || gdt.getDayOfWeekLocalTime() == "2" || gdt.getDayOfWeekLocalTime() == "3" || gdt.getDayOfWeekLocalTime() == "4" || gdt.getDayOfWeekLocalTime() == "5") {

        var h = new GlideDateTime(endofMonth);
        gs.info('RG:: is day of the week ' + h);

        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(h, today);
        var u = dur.getRoundedDayPart();
        gs.info('RG :SUBTRACT: ' + u);

        if (u == 0) {
            gs.info('RG: is equal to 0  : h');
            answer = true;
        }
        // else {
        //     return false;
        // }
    } else {
        gs.info('RG:: false');
        answer = false;
    }
    return answer;
}

 

If it still doesn't work, can you share a whole screenshot of the Scheduled Job (including other condition, 'run this script' section)?

View solution in original post

4 REPLIES 4

James Chun
Kilo Patron

Hey @Gpope,

 

Given that the checkdate function is working, update the first two lines of the condition as below:

var answer = checkdate();
answer;

 Ref - https://developer.servicenow.com/dev.do#!/learn/courses/utah/app_store_learnv2_automatingapps_utah_a...

 

Cheers

Gpope
Tera Contributor

Hi @James Chun , thanks for taking your time in responding me. 

 

I updated this lines but still not working. This is what I get in scripts background : 

Gpope_0-1710270980447.png

 

Hey @Gpope,

 

No worries, it works in my PDI, the condition is:

var answer = checkdate();
answer;
function checkdate() {
    var answer = false;
    var today = new GlideDateTime('2024-03-01 18:38:34');
    gs.info(' RG:: Today is: ' + today);

    var endofMonth = new GlideDateTime(today);
    endofMonth.setDayOfMonthLocalTime(1);
    gs.info(' RG:: FIRST DAY of month is: ' + endofMonth);

    var gdt = new GlideDateTime(endofMonth);
    gdt.getDayOfWeekLocalTime()
    gs.info(' RG:: Day of Week: ' + gdt.getDayOfWeekLocalTime());

    if (gdt.getDayOfWeekLocalTime() == "6") {
        var l = new GlideDateTime(endofMonth);
        l.addDaysLocalTime(2);
        gs.info('RG::' + l);
        var dura = new GlideDuration();
        dura = GlideDateTime.subtract(l, today);
        var i = dura.getRoundedDayPart();
        gs.info('RG::' + i);
        if (i == 0) {
            gs.info(' RG:: l');
            answer = true;
        }
        // else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "7") {
        var k = new GlideDateTime(endofMonth);
        k.addDaysLocalTime(1);
        gs.info('RG::' + k);
        var durat = new GlideDuration();
        durat = GlideDateTime.subtract(k, today);
        var o = dur.getRoundedDayPart();
        gs.info('RG::' + o);
        if (o == 0) {
            gs.info('RG:: k');
            answer = true;
        }
        //else {
        //    return false;
        //}
    } else if (gdt.getDayOfWeekLocalTime() == "1" || gdt.getDayOfWeekLocalTime() == "2" || gdt.getDayOfWeekLocalTime() == "3" || gdt.getDayOfWeekLocalTime() == "4" || gdt.getDayOfWeekLocalTime() == "5") {

        var h = new GlideDateTime(endofMonth);
        gs.info('RG:: is day of the week ' + h);

        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(h, today);
        var u = dur.getRoundedDayPart();
        gs.info('RG :SUBTRACT: ' + u);

        if (u == 0) {
            gs.info('RG: is equal to 0  : h');
            answer = true;
        }
        // else {
        //     return false;
        // }
    } else {
        gs.info('RG:: false');
        answer = false;
    }
    return answer;
}

 

If it still doesn't work, can you share a whole screenshot of the Scheduled Job (including other condition, 'run this script' section)?

Gpope
Tera Contributor

Thanks @James Chun  ! This helped me !