Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Flow Cleanup

ccarver
Tera Contributor

Looking for advice, cautions, gotchas, etc. etc.. around cleaning up orphaned flows associated with task records that are not active. As admins how do you get orphaned flows to a minimum?

Here is a script I am using to explore flows associated with task records, where the task record is no longer active.

var grFlowContext = new GlideRecord('sys_flow_context');

//grFlowContext.addQuery('state', 'WAITING');
//grFlowContext.addQuery('state', 'QUEUED');
//grFlowContext.addQuery('state', 'IN_PROGRESS');
//grFlowContext.addQuery('state', 'PAUSED_IN_DEBUG');
//grFlowContext.addQuery('state', 'PAUSED');
//grFlowContext.addQuery('state', 'CONTINUE_SYNC');

grFlowContext.addQuery('state', 'WAITING').addOrCondition('state','QUEUED').addOrCondition('state','IN_PROGRESS').addOrCondition('state','PAUSED_IN_DEBUG').addOrCondition('state','PAUSED').addOrCondition('state','CONTINUE_SYNC');
grFlowContext.query();

var total = 0;
var found = 0;
while (grFlowContext.next()) {
    try {
        total = total + 1;
        var sourceRecord = new GlideRecord(grFlowContext.source_table);
        if (sourceRecord.get(grFlowContext.source_record)) {
            if (!sourceRecord.active &&
                (grFlowContext.source_table == 'incident' ||
                    grFlowContext.source_table == 'change_request' ||
                    grFlowContext.source_table == 'sc_req_item' ||
                    grFlowContext.source_table == 'sc_task')) {
                found = found + 1;
                gs.print('Name: ' + grFlowContext.name +
                    ' Flow Context ID: ' + grFlowContext.sys_id +
                    ', Ticket Number: ' + sourceRecord.number +
                    ', Created On: ' + sourceRecord.sys_created_on);
            }
        }
    } catch (e) {
        //gs.log('Error processing Flow Context ID: ' + grFlowContext.sys_id + ' - ' + e.message);
    }
}

gs.print('Total: ' + total + ' Found: ' + found);
6 REPLIES 6

ccarver
Tera Contributor

Technically - a flow can continue execution after the closure/completion of the source record it is associated with. I find this practice to be ill advised for the purposes of support and maintenance for production environments. 

In our deployment the rule of thumb for a flow with an associated source record, the record must remain active as long as the flow continues its operation. If a source record closes (becomes inactive), then the flow needs to wrap things up in a timely fashion and complete its operations. 

Caveat - A source record can remain active after the completion of a flow; but the opposite is ill advised practice for our environment. 

So, I was/am performing general maintenance on our instance and found we have roughly 2K orphaned flows where the source record is inactive, but the flow is still in a state of execution. (Some have been in the Waiting state for over 2 years.) Which should not be the case. I am thinking of canceling these flows and seeking other advice from SN admins that have come across this in the wild. 

Syvo
Giga Guru

Hi,

I'm in a similar situation where 60k flows are waiting, some for more than a year when we were not even live. Not sure if it has an impact or anything, I just don't like to have them around.

@ccarverdid you continue your work around this clean up task?