Run script activity showing executing

SHALIKAS
Tera Guru

I have written the following run script but it is stuck at run script activity only

 

var start=new GlideDate(current.variables.start_date);
var end=new GlideDate(current.variables.end_date);
var recurrence=current.variables.set;
var repeatEvery= parseInt(current.variables.repeat_every,10);
//var selectedWeekday= current.variables.weekly ? parseInt(current.variables.weekly,10):null;
var selectedWeekday={};
var dayofMonth=current.variables.on_day ? parseInt(current.variables.on_day,10😞null;

if(current.variables.monday==true) selectedWeekday[7]=true;
if(current.variables.tuesday==true) selectedWeekday[1]=true;
if(current.variables.wednesday==true) selectedWeekday[2]=true;
if(current.variables.thursday==true) selectedWeekday[3]=true;
if(current.variables.friday==true) selectedWeekday[4]=true;
if(current.variables.saturday==true) selectedWeekday[5]=true;
if(current.variables.sunday==true) selectedWeekday[6]=true;

var maxLoop=500;
var count=0;
  var currentDate=new GlideDate(start);
var endDate=new GlideDate(end);
if(recurrence=='Weekly' && selectedWeekday!==null)
{
    while(currentDate.getDayOfWeek() != selectedWeekday)
    {
        currentDate.addDays(1);
    }
}

if(recurrence =='Monthly' && dayofMonth)
{
    var y=currentDate.getYearLocalTime();
    var m=currentDate.getMonthLocalTime();  
    var day=dayofMonth||currentDate.getMonthLocalTime();
    var dateStr=y+'-'+(m<10?'0'+m:m)+'-'+(day<10?'0'+day:day);
    currentDate=new GlideDate(dateStr);
}
while(currentDate.getValue()<=end.getValue())
{
    count++;
    if(count>maxLoop)
    {
        gs.error('loop stopped');
        break;
    }
    var task=new GlideRecord('sc_task');
    task.initialize();
    task.request_item=current.sys_id;
    task.short_description='Recurring meeting'+currentDate.getDate();
    task.u_app_request_type='Presenation Setup';
    task.description='Please move the task to a Pending state if the meeting is far in the future';
    task.insert();

    if(recurrence=='Daily')
    {
        currentDate.addDays(repeatEvery);
    }
    else if(recurrence=='Weekly')
    {
        currentDate.addDays(repeatEvery*7);
    }
    else if(recurrence=='Monthly')
    {
        currentDate.addMonths(repeatEvery);
       
    }
}
1 ACCEPTED SOLUTION

Itallo Brandão
Mega Guru

Hi SHALIKAS,

The script is getting stuck because of an Infinite Loop inside your Weekly logic block.

The Root Cause: In the line while(currentDate.getDayOfWeek() != selectedWeekday), you are comparing an Integer (the day of the week) with an Object (selectedWeekday). Since 1 (Monday) is never equal to {} (Object), the condition is always true, and currentDate.addDays(1) runs forever until the transaction times out.

The Fix:

  1. Fix the Weekly Logic: You need to check if the current day exists inside the object map using !selectedWeekday[currentDate.getDayOfWeek()].

  2. Fix Syntax Error: There is a typo in the dayofMonth line (10null should be 10) : null).

Here is the corrected script:

 
var start = new GlideDate(current.variables.start_date);
var end = new GlideDate(current.variables.end_date);
var recurrence = current.variables.set;
var repeatEvery = parseInt(current.variables.repeat_every, 10);

var selectedWeekday = {};
// Fix Syntax Error here: '10null' -> '10) : null'
var dayofMonth = current.variables.on_day ? parseInt(current.variables.on_day, 10) : null;

// Populate the map
if (current.variables.monday == true) selectedWeekday[1] = true; // Note: In GlideDate, Monday is usually 1
if (current.variables.tuesday == true) selectedWeekday[2] = true;
if (current.variables.wednesday == true) selectedWeekday[3] = true;
if (current.variables.thursday == true) selectedWeekday[4] = true;
if (current.variables.friday == true) selectedWeekday[5] = true;
if (current.variables.saturday == true) selectedWeekday[6] = true;
if (current.variables.sunday == true) selectedWeekday[7] = true;

var maxLoop = 500;
var count = 0;
var currentDate = new GlideDate(start);
// var endDate = new GlideDate(end); // Not strictly needed as you use end.getValue()

// --- FIX FOR WEEKLY LOOP ---
if (recurrence == 'Weekly') {
    var safeSearch = 0;
    // Keep adding days while the current day of week is NOT in your selected map
    while (!selectedWeekday[currentDate.getDayOfWeek()]) {
        currentDate.addDays(1);
        safeSearch++;
        if (safeSearch > 7) break; // Safety break to prevent infinite loop if no days selected
    }
}

if (recurrence == 'Monthly' && dayofMonth) {
    var y = currentDate.getYearLocalTime();
    var m = currentDate.getMonthLocalTime();
    // Logic to handle specific day of month
    var dateStr = y + '-' + (m < 10 ? '0' + m : m) + '-' + (dayofMonth < 10 ? '0' + dayofMonth : dayofMonth);
    currentDate = new GlideDate(dateStr);
    
    // Ensure we start on or after the start date
    if(currentDate.getValue() < start.getValue()){
        currentDate.addMonths(1);
    }
}

while (currentDate.getValue() <= end.getValue()) {
    count++;
    if (count > maxLoop) {
        gs.error('Script loop stopped due to maxLoop limit');
        break;
    }

    var task = new GlideRecord('sc_task');
    task.initialize();
    task.request_item = current.sys_id;
    task.short_description = 'Recurring meeting ' + currentDate.getDisplayValue(); // Changed to getDisplayValue for readability
    task.u_app_request_type = 'Presenation Setup';
    task.description = 'Please move the task to a Pending state if the meeting is far in the future';
    task.insert();

    if (recurrence == 'Daily') {
        currentDate.addDays(repeatEvery);
    } else if (recurrence == 'Weekly') {
        currentDate.addDays(repeatEvery * 7);
    } else if (recurrence == 'Monthly') {
        currentDate.addMonths(repeatEvery);
    } else {
        // Safety: If recurrence doesn't match, break to avoid infinite loop
        break; 
    }
}



Key Note on getDayOfWeek(): Check your instance configuration. Standard GlideDate usually treats Monday as 1 and Sunday as 7, but I updated your map keys to match standard ISO formats just in case.

Hope this helps!

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron

@SHALIKAS It looks like you script is stuck in a loop. I recommend you to test this script inside a background script and see if it creates an infinite loop.

 

Hope this helps.

Itallo Brandão
Mega Guru

Hi SHALIKAS,

The script is getting stuck because of an Infinite Loop inside your Weekly logic block.

The Root Cause: In the line while(currentDate.getDayOfWeek() != selectedWeekday), you are comparing an Integer (the day of the week) with an Object (selectedWeekday). Since 1 (Monday) is never equal to {} (Object), the condition is always true, and currentDate.addDays(1) runs forever until the transaction times out.

The Fix:

  1. Fix the Weekly Logic: You need to check if the current day exists inside the object map using !selectedWeekday[currentDate.getDayOfWeek()].

  2. Fix Syntax Error: There is a typo in the dayofMonth line (10null should be 10) : null).

Here is the corrected script:

 
var start = new GlideDate(current.variables.start_date);
var end = new GlideDate(current.variables.end_date);
var recurrence = current.variables.set;
var repeatEvery = parseInt(current.variables.repeat_every, 10);

var selectedWeekday = {};
// Fix Syntax Error here: '10null' -> '10) : null'
var dayofMonth = current.variables.on_day ? parseInt(current.variables.on_day, 10) : null;

// Populate the map
if (current.variables.monday == true) selectedWeekday[1] = true; // Note: In GlideDate, Monday is usually 1
if (current.variables.tuesday == true) selectedWeekday[2] = true;
if (current.variables.wednesday == true) selectedWeekday[3] = true;
if (current.variables.thursday == true) selectedWeekday[4] = true;
if (current.variables.friday == true) selectedWeekday[5] = true;
if (current.variables.saturday == true) selectedWeekday[6] = true;
if (current.variables.sunday == true) selectedWeekday[7] = true;

var maxLoop = 500;
var count = 0;
var currentDate = new GlideDate(start);
// var endDate = new GlideDate(end); // Not strictly needed as you use end.getValue()

// --- FIX FOR WEEKLY LOOP ---
if (recurrence == 'Weekly') {
    var safeSearch = 0;
    // Keep adding days while the current day of week is NOT in your selected map
    while (!selectedWeekday[currentDate.getDayOfWeek()]) {
        currentDate.addDays(1);
        safeSearch++;
        if (safeSearch > 7) break; // Safety break to prevent infinite loop if no days selected
    }
}

if (recurrence == 'Monthly' && dayofMonth) {
    var y = currentDate.getYearLocalTime();
    var m = currentDate.getMonthLocalTime();
    // Logic to handle specific day of month
    var dateStr = y + '-' + (m < 10 ? '0' + m : m) + '-' + (dayofMonth < 10 ? '0' + dayofMonth : dayofMonth);
    currentDate = new GlideDate(dateStr);
    
    // Ensure we start on or after the start date
    if(currentDate.getValue() < start.getValue()){
        currentDate.addMonths(1);
    }
}

while (currentDate.getValue() <= end.getValue()) {
    count++;
    if (count > maxLoop) {
        gs.error('Script loop stopped due to maxLoop limit');
        break;
    }

    var task = new GlideRecord('sc_task');
    task.initialize();
    task.request_item = current.sys_id;
    task.short_description = 'Recurring meeting ' + currentDate.getDisplayValue(); // Changed to getDisplayValue for readability
    task.u_app_request_type = 'Presenation Setup';
    task.description = 'Please move the task to a Pending state if the meeting is far in the future';
    task.insert();

    if (recurrence == 'Daily') {
        currentDate.addDays(repeatEvery);
    } else if (recurrence == 'Weekly') {
        currentDate.addDays(repeatEvery * 7);
    } else if (recurrence == 'Monthly') {
        currentDate.addMonths(repeatEvery);
    } else {
        // Safety: If recurrence doesn't match, break to avoid infinite loop
        break; 
    }
}



Key Note on getDayOfWeek(): Check your instance configuration. Standard GlideDate usually treats Monday as 1 and Sunday as 7, but I updated your map keys to match standard ISO formats just in case.

Hope this helps!