How Can I Call A Workflow From A Script?

Casey23
Tera Guru

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)
1 ACCEPTED SOLUTION

Casey23
Tera Guru

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!

View solution in original post

10 REPLIES 10

I printed the count of the records (total found was 3) that were caught in the query and that aligns with the 3 records in the table.

Casey23
Tera Guru

For some reason I can't seem to kill or delete the transaction in the Active Transactions table, so I'm still working on that. Or hoping that the script will stop itself after 4 hours since I had the box checked when running it.

On a related note, I went out to my developer instance and ran a some tests with the modified version of the script below and it was sort of successful.

Without the workflow piece in the while loop, both records are located. However, with the workflow piece in place, it only creates one instance of the workflow meaning that the other record isn't having a workflow started for it. Also, this behavior is the same whether I use "new Workflow();" or "new global.Workflow();". 

So with the original issue, it almost seems like the encoded query is doing something weird as Jaspal suggested. But also it seems like the workflow piece is going to be an issue too.

var gr = new GlideRecord('incident');
var count = 0;
var strQuery = 'number=INC0010002';
strQuery = strQuery + '^ORnumber=INC0010001'
gr.addEncodedQuery(strQuery);
gr.query();
while (gr.next()) {
    count++;
    var wflw = new Workflow();
    wflw.startFlow(wflw.getWorkflowFromName('Service Catalog Item Request '), gr, 'insert');
}
gs.log("This is how many records were found: " + count)

Casey23
Tera Guru

Adding more confusion to this, I changed the query in my developer instance to similarly match how I have the query setup in our pre-prod instances. But since I don't have the same table in my developer instance, I stuck with the incident table. For whatever reason, this query doesn't seem to have the issue where the script won't stop running and creating records in the wf_context table. Instead, it works correctly to get my count, but then when I call the workflow it only starts one workflow instead of 2 (the same two incidents from my previous post are returned in the query).

var gr = new GlideRecord('incident');
var count = 0;
var strQuery = 'sys_created_onRELATIVEGT@dayofweek@ago@413';
strQuery = strQuery + '^sys_created_onRELATIVELT@dayofweek@ago@411'
gr.addEncodedQuery(strQuery);
gr.query();
while (gr.next()) {
    count++;
    var wflw = new Workflow(); //new global.Workflow();
    wflw.startFlow(wflw.getWorkflowFromName('Service Catalog Item Request'), gr, 'insert');
}
gs.log("This is how many records were found: " + count)

Casey23
Tera Guru

Just bumping this to see if anyone has been able to trigger a workflow more than once via script? Almost every community article I come across shows the method above (or a variation of it) to trigger the workflow, but it seemingly won't work more than once.

Casey23
Tera Guru

I haven't given up on this yet. 🙂 

I still have no idea why this script doesn't work correctly when calling a workflow. However, I found that when calling a flow, it works correctly. This should actually work as a solution for me, but I need to do some additional testing and learning in order to be sure of that. For reference, the working script is below and I'll be back to report my success, or ask for more assistance if needed.

var gr = new GlideRecord('incident');
var count = 0;
var strQuery = 'sys_created_onRELATIVEGT@dayofweek@ago@420';
//var wflw = new Workflow(); //new global.Workflow();
var inputs = {};
inputs['current'] = gr;
inputs['table_name'] = 'incident';
strQuery = strQuery + '^sys_created_onRELATIVELT@dayofweek@ago@418';
gr.addEncodedQuery(strQuery);
gr.query();
while (gr.next()) {
    count++;
    //wflw.startFlow(wflw.getWorkflowFromName('Service Catalog Item Request'), gr, 'insert');
    //new Workflow().startFlow('0287f2c64a36232700820846b1f8bdce', current, 'insert');
    sn_fd.FlowAPI.executeFlow('global.change__standard', inputs)
    gs.log("HOW MANY TIMES DID WE GET HERE: " + count);
    
}
gs.log("This is how many records were found: " + count);