Creating a scheduled job in ServiceNow to extend the "Valid to" date of articles that will expire within the next 30 days involves a few steps. You'll need to create a Scripted Scheduled Job that runs monthly and checks for articles nearing expiry. If an article's expiry is within the next 30 days, the script will extend the "Valid to" date by 45 days.
Below is a step-by-step guide on how to accomplish this:
1. Navigate to the Scheduled Jobs Module
First, go to System Definition > Scheduled Jobs in your ServiceNow instance.
2. Create New Scheduled Job
Click on "New" to create a new job. Click on "Advanced" to open the advanced view where you can write a script.
3. Configure Your Scheduled Job
Give your job a descriptive name, such as "Extend Article Validity". Then, specify that it should run monthly. Under the "When to run" tab, you might choose "Run" as "Periodically" and then set it up to run monthly. Depending on your requirements, you might be precise with the day and time, depending on when you expect the least impact on users or system performance.
4. Script the Logic
Under the "Run" tab, you'll find a script box where you can insert your custom script. Here is an example script that would accomplish what you're looking to do:
(function executeRule(current) {
// Calculate the date 30 days from now
var thirtyDaysFromNow = gs.daysAgo(-30);
// Query for articles with 'valid_to' date within the next 30 days
var kbArticle = new GlideRecord('kb_knowledge');
kbArticle.addQuery('valid_to', '<=', thirtyDaysFromNow);
kbArticle.query();
while (kbArticle.next()) {
// For each article, calculate the new 'valid_to' date, 45 days from the current 'valid_to' date
var validToDate = kbArticle.getValue('valid_to');
var newValidToDate = new GlideDateTime(validToDate); // Create GlideDateTime instance with current 'valid to' date
newValidToDate.addDays(45); // Add 45 days to it
// Update the 'valid_to' field with the new date
kbArticle.setValue('valid_to', newValidToDate);
kbArticle.update();
gs.info("Extended 'valid_to' date for article: " + kbArticle.getDisplayValue('number') + " to " + newValidToDate.getByFormat("yyyy-MM-dd"));
}
})(current);
Please review and adjust the script according to the exact table and field names used in your ServiceNow instance, as names like 'kb_knowledge' and 'valid_to' may differ based on configuration or version.
5. Save the Scheduled Job
After entering and reviewing your script to ensure it does what you need, save the Scheduled Job.
Testing and Validation
Before relying on this job in a production environment, you should thoroughly test it in a development or testing instance. Create some test articles set to expire within the next 30 days and verify if their "Valid to" dates are correctly extended by 45 days after the job runs.
Note
The script provided is a basic example. Depending on your specific requirements and ServiceNow setup, you might need to adjust or expand upon it. Additionally, always ensure that you have backups and have tested thoroughly before implementing new scheduled jobs in a production environment.