- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 03:07 AM
Hi,
I'm curiouse how can I create loop over workflow by script. I've got run script thats sends email and weekly timer. I wanna loop it four times until end of the month and then go to begining. I didn't find any out-of-the-box solution in the way of activities for loop. Below is my run script to send emails
var callers = [];
var encodedString = 'active=true^state=-5';
var gr = new GlideRecord("change_request");
gr.addQuery(encodedString);
gr.query();
while (gr.next()) {
callers.push(gr.requested_by.email.toString());
}
var arrayUtil = new ArrayUtil();
callers = arrayUtil.unique(callers);
for(var i = 0; i < callers.length; i++){
gs.eventQueue('reminder.event', null, callers[i], '');
}
Maybe there is another simpler way to solve this ?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 09:13 PM
Hi
If I stay on a scheduled job, there are more options.
a) you create different schedulef jobs and they run on different schedules (not sure, if you can configure a workflow to only run at month end)
OR
b) you handle your scheduling logic INSIDE ONE scheduled job in a script which runs DAILY , e.g. To check the last day of the month and then run the specific logic.
Personally I would choose option b)
This would keep all logic regarding your one requirement one one place.
And remember, that workflows are not intended to run forever.
Let me know if that answered your question and mark my answer as correct and helpful.
BR Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 03:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 01:41 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 03:36 PM
Yes, sorry I pictured the Timer activity going to End instead of back to the Run Script. Your revised workflow with the turnstile of 4 will send 5 emails and then end. The workflow will start running when each change request record is created. Your query should also include the current record since this will be running on each change record
var encodedString = 'active=true^state=-5^sys_id=' + current.sys_id.toString();
Then technically this is an encoded query, even though addQuery will work, so you could just skip the var line and
gs.addEncodedQuery('active=true^state=-5^sys_id=' + current.sys_id.toString());
Since you are only sending the email if the State is New, you could also add an If activity after the Timer, with the Condition State is New. Then the paths leading out of that activity would be Yes -> Turnstile, or No -> End.
If what you really want to do is trigger a weekly email to every Requested by of a Change that is in the New State, then you can instead create a scheduled job. On the interceptor, select "Automatically run a script of your choosing", then just give it a Name and when you want it to run.
The script would be the same as your workflow script, if that was working for you.
var callers = [];
var gr = new GlideRecord("change_request");
gr.addEncodedQuery('active=true^state=-5');
gr.query();
while (gr.next()) {
callers.push(gr.requested_by.email.toString());
}
var arrayUtil = new ArrayUtil();
callers = arrayUtil.unique(callers);
for(var i = 0; i < callers.length; i++){
gs.eventQueue('reminder.event', null, callers[i], '');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2020 03:25 AM
Hi there,
Two possibilities jump in mind for this:
- Utility "Turnstile", on which you add an Integer, for how many times this should loop over;
- Adding an "If" utility with script, script which uses workflow.scratchpad. In an earlier Utility (or workflow input) you should define the number of times to loop.
Turnstile would be easiest. If utility would give you additional possibilities in scripting, like leaving the loop earlier if a different condition already matches.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field