How To Tell When A Workflow Was Used Last?

Casey23
Tera Guru

Hello,

I'm wondering if someone can tell me how to see when the last time was that a workflow was used? I keep seeing references to the wf_context table, which shows me some workflows and the last time they were triggered, but that still requires a lot of manual effort to look at dates and compare workflows in that table to the amount of workflows in our system. Ideally, I'd like to have a report that shows workflows not used in the last 6 months so we can review them and see if they can be set to inactive. Has anyone created something like that before?

1 ACCEPTED SOLUTION

Sure it would be easy enough to modify the script I previously provided to check for name instead of sys_id, see updated script below. Also -- if any of my answers in this thread have been helpful or solved your question, I would really appreciate it if you mark them as Helpful and/or Correct!

checkWorkflows();

function checkWorkflows() {
    var publishedWfGr = new GlideRecord('wf_workflow_version');
    publishedWfGr.addEncodedQuery('published=true');
    publishedWfGr.query();
    var staleWfList = [];
    while (publishedWfGr.next()) {
        var wfContextGr = new GlideRecord('wf_context');
        wfContextGr.addEncodedQuery(gs.getMessage('workflow_version.name={0}^sys_created_onONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()', publishedWfGr.getValue('name')));
        wfContextGr.setLimit(1);
        wfContextGr.orderByDesc('sys_created_on');
        wfContextGr.query();
        if (!wfContextGr.next()) {
            staleWfList.push('Published workflow has not been triggered in past 6 months: ' + publishedWfGr.getDisplayValue());
        }
    }
    var resultSummary = staleWfList.join('\n');
    if (resultSummary) {
        gs.info(resultSummary);
        //could you gs.eventQueue() here instead to trigger scheduled email, include resultSummary as event.parm1 or event.parm2
    }
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

8 REPLIES 8

chrisperry
Giga Sage

Hi Casey,

What kind of workflows specifically? Are you referring to workflows for service catalog items (sc_req_item)?

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hello!

I'm looking for anything that shows up in the workflow editor as published. Most are sc_req_item, but we have 55 workflows that are for a variety of tables that aren't sc_req_item.

Got it - so what you could do is set up a scheduled script execution to run however often you need, for example once monthly as below:

find_real_file.png

In the script we go find all published workflows, and then iterate through each one to see if it has been triggered in the past 6 months, aka does the workflow have a wf_context record created in the last 6 months. If the workflow does not have a wf_context record in the past 6 months then we will add it to an array to print the results at the end of the script. As I commented in the code you could replace the gs.info() with gs.eventQueue() to trigger an email notification on a scheduled basis with the script results:

script:

checkWorkflows();

function checkWorkflows() {
    var publishedWfGr = new GlideRecord('wf_workflow_version');
    publishedWfGr.addEncodedQuery('published=true');
    publishedWfGr.query();
    var staleWfList = [];
    while (publishedWfGr.next()) {
        var wfContextGr = new GlideRecord('wf_context');
        wfContextGr.addEncodedQuery(gs.getMessage('workflow_version={0}^sys_created_onONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()', publishedWfGr.getUniqueValue()));
        wfContextGr.query();
        if (!wfContextGr.hasNext()) {
            staleWfList.push('Published workflow has not been triggered in past 6 months: ' + publishedWfGr.getDisplayValue());
        }
    }
    var resultSummary = staleWfList.join('\n');
    if (resultSummary) {
        gs.info(resultSummary);
        //could use gs.eventQueue() here instead to trigger scheduled email, include resultSummary as event.parm1 or event.parm2
    }
}

result in my PDI:

Published workflow has not been triggered in past 6 months: Change Request - Normal change tasks
Published workflow has not been triggered in past 6 months: Knowledge - Instant Publish
Published workflow has not been triggered in past 6 months: Grant role_delegator role to user in group
Published workflow has not been triggered in past 6 months: Knowledge - Approval Publish
Published workflow has not been triggered in past 6 months: HR Case Approval Subflow
Published workflow has not been triggered in past 6 months: Service Task Processing
Published workflow has not been triggered in past 6 months: Pwd Reset - Local ServiceNow
Published workflow has not been triggered in past 6 months: Item Designer - Approvals
Published workflow has not been triggered in past 6 months: HRI Corporate Credit Card Request
Published workflow has not been triggered in past 6 months: On-Call: Escalations By Email Per Rota
Published workflow has not been triggered in past 6 months: Item Designer - generate approvals for current sequence
Published workflow has not been triggered in past 6 months: Pwd Reset - Master
Published workflow has not been triggered in past 6 months: Change Request - Standard
Published workflow has not been triggered in past 6 months: Delegate roles to group member

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Thank you for the reply! I ran this script, and for the most part, it worked great. However, I was manually reviewing a handful on the list just to see if I could find any discrepancies and noticed a couple.

We have two workflows which I can see have had workflows triggered in the last 6 months. In fact one of them had a bunch that were triggered today. So I'm curious why those would have showed up in the list? I attached images of the two workflows in question so you could see what I am talking about. This screen shot is from the Workflow Contexts table.