How to trigger workflow only on "Workflow scheduled". not when record inserted / updated in table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2024 02:48 AM - edited 01-15-2024 02:48 AM
Hi All,
I had one requirement. we were importing 100k records into snow using import process. My requirement was to send email at end of the day when import process is completed. So I created a workflow on sys_import_set_run table to trigger a notification and I created a scheduled workflow (at 11:30 PM) where it will trigger my workflow.
My expectation was this workflow should trigger at 11:30 as per triggering condition. But it is triggering every time when record get inserted in sys_import_set_run table.
How to achieve this using other solution.
Thank all,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2024 06:11 AM
Hi @Obito
To achieve the requirement of sending an email at the end of the day when the import process is completed, rather than triggering a workflow on each record insertion into the `sys_import_set_run` table, you can use a scheduled job that runs once a day at the specified time. Here's how you can set up a scheduled job in ServiceNow:
Create Scheduled Job:
- Navigate to System Definition > Scheduled Jobs
- Click on "New" to create a new scheduled job.
- Select "Automatically run a script of your choosing" for "What would you like to automate?"
- Fill in the appropriate fields such as Name, Run, and Time (set this to run daily at 11:30 PM).
- In 'Run this script' field, write a script that checks the `sys_import_set_run` table for completed import sets for that day and sends an email notification if the condition is met.
(function executeRule(current, previous /*null when async*/) {
// Query the sys_import_set_run table for completed imports of the day
var grImportSetRun = new GlideRecord('sys_import_set_run');
grImportSetRun.addQuery('state', 'Complete'); // Assuming 'Complete' is the state for finished imports
grImportSetRun.addQuery('sys_created_on', 'ON', 'today'); // Adjust the field if necessary
grImportSetRun.query();
if (grImportSetRun.hasNext()) {
// Send email notification
var notification = new GlideNotification();
notification.send('import_completed_notification', grImportSetRun); // Replace with your notification event name
}
})();
- Replace 'import_completed_notification' with the actual event name you have configured for your email notification.
By using a scripted scheduled job, you ensure that the check for completed imports and the sending of the email only happens once a day at the specified time, rather than on each record insertion. This approach is more efficient and aligns with your requirement.
Please mark this response as correct or helpful if it assisted you with your question.