- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 10:54 AM
I am wondering how I can start a workflow from a script? I've seen various examples on community, but nothing that's really making sense for what I'm attempting to do. In my script, I'm looking for records that are going to expire in 60 days. That piece of the script is working great and my count of 3 that's returned after running is accurate. However, I'm then trying to trigger a workflow for each of those records that are found so I can assign tasks to a particular group. When I tried the script below, the system ended up starting roughly 5,500 instances of the workflow. We only have about 400 records in the u_cmdb_ci_certificates table, so I'm confused as to why that happened considering those numbers don't line up and I only had 3 returned results from the GlideRecord.
For bonus points, I'll also need to pass the name and certificate_expiration fields to the workflow so that teams who received the tasks know which certificate to look at.
Here is what I have so far:
var gr = new GlideRecord('u_cmdb_ci_certificates');
var count = 0;
var strQuery = 'u_certificate_expirationRELATIVEGT@dayofweek@ahead@59';
strQuery = strQuery + '^u_certificate_expirationRELATIVELT@dayofweek@ahead@61'
strQuery = strQuery + '^u_retired=false'
gr.addEncodedQuery(strQuery);
gr.query();
while (gr.next()) {
count++;
var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('Certificate Expiration'), gr, 'insert');
}
gs.log("This is how many records were found: " + count)
Solved! Go to Solution.
- Labels:
-
Workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2022 11:58 AM
While the back story of how this script started is way too long to get into, I wanted to say that I sort of gave up on the script after running into lots of issues.
However what I'm doing is basically the exact same query, but I'm doing it in Flow Designer. From there, for each of those records, I'm submitting a catalog request. The catalog request then has the workflow tied to it. So from the standpoint of what I was trying to do above, for every record that is about to expire, the workflow is triggered.
Hope this helps someone looking to do something similar!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 11:01 AM
Code looks alright,
just try updating the line
var wflw = new global.Workflow();
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 11:14 AM
I ended up with the same result after trying that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 12:43 PM
Adding to this issue, I just noticed that the background script is just continuously starting new workflows. I just checked in the Workflow Contexts table and there are nearly 70,000 started workflows. I am in the process of working on cancelling this, but something in the code above is clearly wrong.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 01:06 PM
That something makes me think the EncodedQuery being passed has an issue.
Did you try printing result for the query to see how many records you get once so as to confirm if the query returns what is expected.