How to setup a scheduled job to run when there is an active record found on the task table?

ashpan
Tera Contributor

Hello,

 

I have a requirement where if there is an active record found on task table assigned to a specific group, the scheduled job should execute and mark the record closed and inactive. I was able to achieve the 2nd part of the script to close the tasks as below:

 

var filterquery = "assignment_group=<sys_id of the group>^active=true";
var gr = new GlideRecord("task");

gr.addEncodedQuery(filterquery);
gr.setLimit(500);

gr.query();

while(gr.next())
{
gr.state='3';
gr.assigned_to="Test User";
gr.update();
}

 

I need some help to get the code which would keep checking the table for any ACTIVE task assigned to the group and as soon as it finds one, the job should close the task.

 

Also,  this job should execute only on Saturday and at 15 minute interval.

 

Any help on this would be appreciated.

 

Regards

 

1 ACCEPTED SOLUTION

Shivalika
Mega Sage

Hello @ashpan 

 

Please add the below script in the scheduled job - 

 

(function executeScheduleJob() {

    var today = new GlideDateTime();

    var dayOfWeek = today.getDayOfWeek(); // 1 = Monday, 7 = Sunday

 

    if (dayOfWeek != 7) { // 7 means Saturday (modify if needed)

        gs.info("Scheduled job skipped - Not Saturday");

        return; 

    }

 

    var groupSysId = "<sys_id_of_the_group>"; // Replace with actual sys_id of the assignment group

    var gr = new GlideRecord("task");

    

    gr.addQuery("assignment_group", groupSysId);

    gr.addQuery("active", true);

    gr.setLimit(500);

    gr.query();

 

    while (gr.next()) {

        gr.state = 3; // 3 = Closed Complete (Modify as per required state)

        gr.active = false;

        gr.assigned_to.setDisplayValue("Test User"); // Modify as needed

        gr.update();

    }

 

    gs.info("Scheduled job executed - Closed tasks for group

: " + groupSysId);

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOw

NeEISQCY

})();

View solution in original post

4 REPLIES 4

Shivalika
Mega Sage

Hello @ashpan 

 

Please add the below script in the scheduled job - 

 

(function executeScheduleJob() {

    var today = new GlideDateTime();

    var dayOfWeek = today.getDayOfWeek(); // 1 = Monday, 7 = Sunday

 

    if (dayOfWeek != 7) { // 7 means Saturday (modify if needed)

        gs.info("Scheduled job skipped - Not Saturday");

        return; 

    }

 

    var groupSysId = "<sys_id_of_the_group>"; // Replace with actual sys_id of the assignment group

    var gr = new GlideRecord("task");

    

    gr.addQuery("assignment_group", groupSysId);

    gr.addQuery("active", true);

    gr.setLimit(500);

    gr.query();

 

    while (gr.next()) {

        gr.state = 3; // 3 = Closed Complete (Modify as per required state)

        gr.active = false;

        gr.assigned_to.setDisplayValue("Test User"); // Modify as needed

        gr.update();

    }

 

    gs.info("Scheduled job executed - Closed tasks for group

: " + groupSysId);

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOw

NeEISQCY

})();

ashpan
Tera Contributor

Thanks Shivalika.
I was able to achieve the code with the help of your suggestion.

Viraj Hudlikar
Giga Sage

Hello @ashpan 

 

Code something can be like this 

//Note: Make sure to replace <sys_id of the group> with the actual sys_id of the group you are targeting.

var filterquery = "assignment_group=<sys_id of the group>^active=true";
    var gr = new GlideRecord("task");
    gr.addEncodedQuery(filterquery);
    gr.setLimit(500);
    gr.query();
    while(gr.next()) {
        gr.state = '3'; // Assuming '3' is the state for 'Closed'
        gr.active = false;
        gr.assigned_to = "Test User"; // Update as needed or pass sys_id of user
        gr.update();
    }

 

Setup your scheduled job as below 

  • Set the Run field to Weekly.
  • Select Saturday and set the Time to the desired start time.
  • Set the Repeat Interval to 15 minutes.

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

priyatam_pvp
Tera Guru

Please check the below Image

code :

var now = new GlideDateTime();
var dayOfWeek = now.getDayOfWeekUTC();
if (dayOfWeek == 7) {// to check if it is saturday
// write you code here
}
 



priyatam_pvp_0-1742394378893.png



Please Mark it helpful

Regards
Priyatam