Custom Discovery schedule using On Condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 07:12 AM
Hi All,
I am trying get my discovery schedule run between specific hour for example :after 5 a.m. . Suppose we have discovery schedule running daily or periodic between 3a.m -5 a.m . The discovery schedule should trigger or run after 5 a.m.
I am trying to run the discovery schedule using the script but the discovery schedule is not running at all .
Note : We have system time zone as MST.
I tried to run for specific days . It worked but for specific day . It did not worked for specific time.
(function() {
var day = new Date().getDay();
return (day == 1 || day == 3);
})();
Please find the script below :
Please help in this regard.
Regards,
Aquib
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 08:38 AM
Hi Aquib,
It looks like you're trying to configure a discovery schedule that runs only after 5 a.m. MST using the "On Condition" option. While your script logic is on the right track, there are a few things to consider to ensure it works as expected. Let me guide you through the process:
Key Points to Consider:
- Server Time Zone:
The `new Date()` function in your script will use the server's time zone. Since your system time zone is MST, the script will already evaluate the time in MST. No additional adjustments are needed.
2. "On Condition" Script Logic:
The script you provided is correct for checking the current hour. However, the "On Condition" script only determines whether the schedule should run at the time it is evaluated. It does not continuously monitor the time. This means the schedule must already be active for the script to evaluate and allow execution.
3. Discovery Schedule Timing:
Ensure that the discovery schedule is configured to run periodically or daily, and the "On Condition" script is used to restrict execution to the desired time window.
Correct Script for "On Condition":
Here’s the script you can use to ensure the discovery schedule runs only after 5 a.m. MST:
(function() {
var currentHour = new Date().getHours(); // Gets the current hour in MST
return (currentHour >= 5); // Returns true if the current time is 5 a.m. or later
})();
Steps to Configure the Discovery Schedule:
- Create or Edit the Discovery Schedule:
- Navigate to Discovery > Discovery Schedules in your ServiceNow instance.
- Open the schedule you want to modify or create a new one.
2. Set the Schedule Timing:
- Configure the schedule to run daily or periodically (e.g., every hour or every few minutes).
- This ensures the schedule is active and the "On Condition" script is evaluated.
3. Add the "On Condition" Script:
- In the Trigger Condition section, select On Condition.
- Paste the script provided above into the script editor.
4. Save and Test:
- Save the schedule and monitor its behavior. The discovery schedule should now only execute after 5 a.m. MST.
Troubleshooting Tips:
- Verify the Server Time:
Use the following script in the Script Editor to confirm the server's current time:
gs.print(new Date());
Ensure the time matches your expectations for MST.
- Check the Schedule Logs:
Navigate to Discovery > Discovery Status to verify if the schedule is being triggered as expected. If it’s not running, ensure the schedule is active and the script is returning true after 5 a.m.
- Test the Script:
Temporarily modify the script to return true for the current hour to confirm that the schedule triggers correctly:
(function() {
return true; // Always returns true for testing purposes
})();
Additional Reference:
For more details on using condition scripts in discovery schedules, check out the following URL:
https://www.servicenow.com/community/itom-forum/custom-discovery-schedule-with-a-condition-script/m-...
This article provides examples of condition scripts for specific days and times, such as running on weekends, skipping Sundays, or running on specific weekdays.
Final Note:
If this solution helps, please mark it as Helpful and Accept it as a Solution so that others in the community can benefit from it.
Let me know if you need further assistance!
Best regards,
Selva Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 11:18 AM
Hi @Selva Arun ,
Thank you for the update. As suggested, I have done the configuration but still it is not working
Run -Daily
Time (run_time) - 3 hours -->
script :
Regards,
Aquib
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 12:35 PM
Hi,
Did you look at the community post https://www.servicenow.com/community/itom-forum/custom-discovery-schedule-with-a-condition-script/m-...
Regards,
Selva Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-04-2025 01:06 AM
this make no sense to run schedule after 5 AM, becase it will be 05:00:01 sec. Always. So you can set it up to that time. Or you are expecting that SN will choose once 05:15:06 and once 05:02:11?? It is not workign like that.