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);
5 REPLIES 5

WillieW
Tera Contributor

Hi,

 

I see the following from running your script in my PDI

Time: 0:00:00.132 id: dev309441_1[glide.3] primary_hash=763426233 (connpid=78167) for: SELECT sys_restricted_caller_access0.`description`, sys_restricted_caller_access0.`target_scope`, sys_restricted_caller_access0.`source`, sys_metadata0.`sys_updated_on`, sys_metadata0.`sys_class_name`, sys_restricted_caller_access0.`rca_type`, sys_metadata0.`sys_id`, sys_metadata0.`sys_updated_by`, sys_metadata0.`sys_created_on`, sys_restricted_caller_access0.`target_table`, sys_metadata0.`sys_name`, sys_metadata0.`sys_scope`, sys_metadata0.`sys_created_by`, sys_restricted_caller_access0.`target_type`, sys_metadata0.`sys_mod_count`, sys_restricted_caller_access0.`source_type`, sys_restricted_caller_access0.`target`, sys_metadata0.`sys_package`, sys_metadata0.`sys_update_name`, sys_restricted_caller_access0.`source_scope`, sys_restricted_caller_access0.`operation`, sys_restricted_caller_access0.`source_table`, sys_restricted_caller_access0.`status`, sys_metadata0.`sys_policy` FROM (sys_restricted_caller_access sys_restricted_caller_access0  INNER JOIN sys_metadata sys_metadata0 ON sys_restricted_caller_access0.`sys_id` = sys_metadata0.`sys_id` )  WHERE sys_restricted_caller_access0.`source_scope` = 'global' AND sys_metadata0.`sys_scope` = sys_restricted_caller_access0.`target_scope` ORDER BY sys_restricted_caller_access0.`sys_id` /* dev309441002, gs:F53F6DA6479EAE106B3ABB30116D43BC, tx:9cd0792e479aae106b3abb30116d4367, hash:763426233 */ 
Source descriptor is empty while recording access for scope ServiceNow Key Management Framework: no thrown error
Security restricted: Read operation on scope 'ServiceNow Key Management Framework' from scope 'Global' was denied because the source could not be found. Please contact the application admin.
Source descriptor is empty while recording access for scope ServiceNow Key Management Framework: no thrown error
Security restricted: Read operation on scope 'ServiceNow Key Management Framework' from scope 'Global' was denied because the source could not be found. Please contact the application admin.
*** Script: Total: 503 Found: 0

Seems some Cross-scope access is needed.

ccarver
Tera Contributor

Had no issues on a fresh PDI instance. my guess is you have some flows operating in scope and you ran this is a different scope. 

Either way the script is sound. 

WillieW
Tera Contributor

Yes, I ran it in Scripts - Background, where only "Global" is available to select for the App scope. And that is due to the limited information on what steps you're using. Hopefully, another member will have suggestions for you. Your script found 161 records in my PDI to process. However none where any in the 4 tables of interest where not active.

WillieW
Tera Contributor

I added some debug to the script, and seems to work as defined. of the 161 records in 'sys_flow_context' table that meet the conditions in line 10. None of the corresponding records in the 4 table incident, change_request, sc_req_item, and sc_task tables have active=false. So Found = 0;

 

What I don't know is how to determine orphaned flows. Relationship between flow context state and active on the source table?