🎓 Scenario-Based Question
Q1: I have a scheduled e-mail report that should run on four specific dates every year:
- 📅 January 1st
- 📅 July 1st
- 📅 December 31st
How can I achieve this in ServiceNow?
✅ Answer:
To achieve this, schedule the report to run daily and apply a conditional script under the “Run this scheduled job only if the following condition is true” option.
Here’s the script you can use:
(function() {
var currentDate = new GlideDateTime();
var month = currentDate.getMonthUTC() + 1; // Adjusted for 0-based index
var day = currentDate.getDayOfMonthUTC();
// Run on Jan 1st, July 1st, or Dec 31st
if ((month === 1 && day === 1) ||
(month === 7 && day === 1) ||
(month === 12 && day === 31)) {
gs.log('✅ Running the scheduled report...');
// You can add custom logic here if needed
return true;
} else {
gs.log('⛔ Not the scheduled date. Month: ' + month + ', Day: ' + day);
return false;
}
})();
📌 Notes:
- Set the schedule to daily in the Scheduled Report setup.
- Ensure “Run this only if condition is true” is checked.
- This script ensures execution only on the exact dates specified.
Q2: Write a Background Script code to link an Incident record to a Problem record?
✅ Answer:
Use the following background script in the Scripts - Background module to associate an Incident with a Problem using its sys_id
:
// Get the Incident record by number and ensure it doesn't already have a problem linked
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0007001'); // Replace with your Incident number
gr.addNullQuery('problem_id');
gr.query();
while (gr.next()) {
// Set the problem_id field to the sys_id of the related problem
gr.problem_id = "62304320731823002728660c4cf6a7e8"; // Replace with actual Problem sys_id
gr.update();
}
Note:
- 🔍 Replace
INC0007001
with the desired Incident number. - 🆔 Replace the
problem_id
with the actual sys_id of the Problem record. - ✔️ Ensure the Problem record exists and the user running the script has update rights.