How To Create A Recurring Task – Use Scheduled Jobs

What Is A Scheduled Job? A scheduled job in ServiceNow is an action (usually a script) that is executed, based on a schedule. Say for example, that you’d like to create an incident, a change …

create a scheduled job

What Is A Scheduled Job?

A scheduled job in ServiceNow is an action (usually a script) that is executed, based on a schedule.

Say for example, that you’d like to create an incident, a change request or a request item on a recurring basis, like daily or weekly – you can create a scheduled job to do that.

The scheduled job covered below will execute a script, every single morning, at 1 AM.

With a scheduled job, you can do things like automate the creation and generation of a report, create any record from a template, or you can even run a custom script – to do whatever you’d like.

The scheduled jobs in ServiceNow run on a recurring basis, and can be run in any of the following schedules (daily, weekly, monthly, periodically, once, on demand and based on calendar schedules):

This can be super helpful you have tasks that need to be performed, on a recurring basis.

In a ServiceNow scheduled job, you can also have a conditional statement, that will return true or false, and the job will only execute if the script returns true.

Scheduled jobs just allow you to leverage the automation of the platform, and I use them frequently.

They can be very helpful when running integrations. Say for example you’d like to pull from Salesforce or Jira, every 6 hours. This automation will allow you to do this, or to keep ServiceNow in-sync with other platforms and software systems.

How To Create A Scheduled Job?

Role Required: admin

In this example, we’re going to create an incident, every morning at 1 AM.

You’ll first go to System Definition > Scheduled Jobs, and click “New”.

To run a script, as we’ll do in this example, select “Automatically run a script of your choosing”.

This will pop-up a blank form, for the scheduled job.

Populate the form, as seen below – this will be the final outcome.

On the form, you’ll see some important fields to be aware of.

You can run them globally, or in a scoped application.

Scheduled jobs can also be conditional, this means that you can have them run or not run, based on a script.

Say for example, you want a scheduled job to run, whether or not a P1 Incident had been created today, you could easily create a GlideRecord object and return true, if one was found or not. The possibilities here based on conditional statements are endless.

A common way to do this is to declare a variable as answer (var answer), and then set that to true or false, and then return it. But any variation of returning a boolean will work.

Write a script to return true or false, if you want it to be conditional

If you’re running a scheduled job, that you always want to execute at a certain time, you’ll probably skip the conditional statement and leave that false.

To run something at 1:00 AM daily, for example, you’d just set the Run field to “Daily” and you’d set the Time to 01 00 00. Then you can decide on the Time Zone too. If you’re not sure, just use the System Time zone.

Here’s how you setup something to run daily at 1 AM

And then to the fun part, the script.

JavaScript To Create An Incident Daily

Here’s the script that will create an incident every single day.

Your use case will likely be a lot more complex than this, so just use this as a framework or a template to build off of.

The “Run This Script” is the code that is executed, based on the schedule that’s defined.

// Create a new record below

var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "Daily Incident For Cleanup";
gr.impact = 2;
gr.urgency = 2;
gr.description = "This is auto created as a reminder to cleanup XYZ...";
gr.insert();

This is an incredibly simple example, and your use case will likely be more complex. But this should get the idea across as to how to create a daily automation, using the ServiceNow platform.

Why Would You Create A Scheduled Job?

Scheduled Jobs are known in engineering as cron jobs. Cron jobs are jobs that execute on a schedule, just like scheduled jobs. I have experience working in both, and there’s usually a complicated formula for creating cron jobs, so I think ServiceNow’s way of doing this is super end-user friendly and is straight forward.

Here are a couple of different use cases for Scheduled Jobs:

Integrations

Integrations are a great use case for scheduled jobs in ServiceNow.

Say for example you want to keep 2 systems in-sync, ServiceNow and another instance.

You can set up automation for ServiceNow to send data to another platform, every X number of hours.

Daily Tasks For Teams

There are a lot of teams in IT and Engineering that have repeatable tasks, based on a schedule.

IT Teams could need to have monthly checks on any number of different systems that they manage.

Being able to leverage the automation in ServiceNow, will allow these teams to get their job done easier, because there’s no need to remember to create an incident to track something.

Any piece of code can be executed on a recurring basis or schedule, so it’s not just to kick off integrations and create incidents.

Anything that can be scripted, can be put into a scheduled job, to improve the processes of your teams.

Let us know some ways that your team below executes scheduled jobs.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x