scheduled job script.

Ubada Barmawar
Giga Guru

Hi ServiceNow Community,

 

I have a task, we have created a custom table called "Training Enquiry" which consists of various fields , one of which is "state" field. the requirement is that I need to write a scheduled job script such that all the training enquiry records which are in "New" state must be set to "Follow up" by the start of next week.

 

hoping to get some useful solution on this.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Ehab Pilloor
Mega Sage

Hi @Ubada Barmawar,

Set the Scheduled Job to weekly and put the day as Monday. Try the following script and correct any values if required. Also you can get a count of records updated in this:

 

var count = 0;
var gr = new GlideRecord('u_training_enquiry');
gr.addQuery('u_state', 'new');
gr.query();
while (gr.next()) {
    gr.setValue('u_state', 'follow_up');
    gr.update();
    count++;
}
gs.log("Count of records updated: " + count);

 

 If you found this reply useful, please mark it as solution/helpful.

 

Thanks and Regards,

Ehab Pilloor

View solution in original post

3 REPLIES 3

Shoheb_IbaaBoss
Tera Guru

Hi,

Try below script:

 

(function executeRule() {

    var trainingEnquiry = new GlideRecord('training_enquiry');
    trainingEnquiry.addQuery('state', 'New');
    trainingEnquiry.query();

    var now = new GlideDateTime();
    var nextWeekStart = new GlideDateTime();
    nextWeekStart.addWeeks(1);
    nextWeekStart.setDayOfWeek(1); // Set to Monday

    while (trainingEnquiry.next()) {
        // Update records with 'Follow up' state
        trainingEnquiry.state = 'Follow up';
        trainingEnquiry.update();
    }

})();

Regards,

Shoheb

Ehab Pilloor
Mega Sage

Hi @Ubada Barmawar,

Set the Scheduled Job to weekly and put the day as Monday. Try the following script and correct any values if required. Also you can get a count of records updated in this:

 

var count = 0;
var gr = new GlideRecord('u_training_enquiry');
gr.addQuery('u_state', 'new');
gr.query();
while (gr.next()) {
    gr.setValue('u_state', 'follow_up');
    gr.update();
    count++;
}
gs.log("Count of records updated: " + count);

 

 If you found this reply useful, please mark it as solution/helpful.

 

Thanks and Regards,

Ehab Pilloor

Amit Verma
Kilo Patron
Kilo Patron

Hi @Ubada Barmawar 

 

Please configure the scheduled job as below :

 

AmitVerma_0-1705486318047.png

 

var tr = new GlideRecord('training_enquiry');
tr.addQuery('state', 'Your New State Value');
tr.query();
while(tr.next()) {
        // Update records with 'Follow up' state
        tr.state = 'Your Follow Up State Value';
        tr.update();
    }

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.