The Zurich release has arrived! Interested in new features and functionalities? Click here for more

background script to identify particular assignment group in all workflows and flow designers

sinu2
Tera Expert

Hi

 

I need to identify the workflows and flow designers that are using particular assignment group- through background script. If anybody has the script kindly help here.

7 REPLIES 7

Aparna42
Tera Contributor
var uniqueRecords = {};

// Define the name of the user group you are looking for
var inputgroupName = agName; // Replace with the actual group name

// Create a new GlideRecord object for the 'sys_user_group' table
var igroupGr = new GlideRecord('sys_user_group');

// Add a query condition to search for the user group by name
igroupGr.addQuery('name', inputgroupName);

// Query the table
igroupGr.query();

if (igroupGr.next()) {
    // If a record is found, get the sys_id of the user group
    var inputAssignmentGroup = igroupGr.getValue('sys_id');
    gs.log('User Group Sys ID: ' + inputAssignmentGroup);
}

//var inputAssignmentGroup = '5573a0801b73e01050a8da02dd4bcb47'; // Your input assignment group value
var gr = new GlideRecord('sys_variable_value');
//variable=d7f2bd33c0a801650021da8ff242a081^valueISEMPTY^ORvalue=feba19191bdfa0545c4699fe034bcbfd
// Add query conditions
var qc = gr.addQuery('value', inputAssignmentGroup);
qc.addOrCondition('value', '');
gr.addQuery('variable', 'd7f2bd33c0a801650021da8ff242a081');

gr.query();

while (gr.next()) {
    var docKey = gr.getValue('document_key');
    //gs.log(docKey);
    var docKeyGr = new GlideRecord('wf_activity');
    if (docKeyGr.get(docKey)) {
        var workflowVersion = docKeyGr.getDisplayValue('workflow_version');
        if (workflowVersion) {
           // gs.log(workflowVersion);
            // Attempt to access vars fields: task_fulfillment_group or advanced_script
            var varsField = gr.getValue('value');
            //gs.log(varsField);
            if (varsField) {
                // Commented out logging
                //gs.log("varsField " + varsField);
               
                var taskFulfillmentGroup = docKeyGr.getDisplayValue('vars.var__m_38891b6f0a0a0b1e00efdfdd77602027.task_fulfillment_group');
               
                // Define the name of the user group you are looking for
                var groupName = taskFulfillmentGroup; // Replace with the actual group name

                // Create a new GlideRecord object for the 'sys_user_group' table
                var groupGr = new GlideRecord('sys_user_group');

                // Add a query condition to search for the user group by name
                groupGr.addQuery('name', groupName);

                // Query the table
                groupGr.query();

                if (groupGr.next()) {
                    // If a record is found, get the sys_id of the user group
                    var groupSysId = groupGr.getValue('sys_id');
                    // Commented out logging
                    // gs.log('User Group Sys ID: ' + groupSysId);
                }

                // Commented out logging
                // gs.log("varsField " + taskFulfillmentGroup);
                if (groupSysId === inputAssignmentGroup) {
                    uniqueRecords[workflowVersion] = true;
                    // Commented out logging
                    // gs.log("Task Fulfillment Group: " + taskFulfillmentGroup);
                }
            } else {
                // Fallback to advanced_script if task_fulfillment_group is not present
                var advancedScript = docKeyGr.getDisplayValue('vars.var__m_38891b6f0a0a0b1e00efdfdd77602027.advanced_script');
                if (advancedScript) {
                    // Extract the assignment group from the advanced script
                    var match = advancedScript.match(/task\.assignment_group\s*=\s*['"]([^'"]+)['"]/);
                    if (match && match[1] === inputAssignmentGroup) {
                        uniqueRecords[workflowVersion] = true;
                        // Commented out logging
                        // gs.log("Extracted Assignment Group: " + match[1]);
                    }
                }
            }
        } else {
            // Commented out logging
            // gs.log("Workflow version not found for Document Key: " + docKey);
        }
    } else {
        // Commented out logging
        // gs.log("No record found for Document Key: " + docKey);
    }
}

var uniqueArray = Object.keys(uniqueRecords);
// Commented out logging
 gs.log("Unique Workflow Versions: " + uniqueArray.join(", "));

// Optional: If you want to fetch the workflow names, you may need additional code here to map workflow versions to names.

Josh_Cooper
Tera Contributor

For Flow, it's harder because of the way ServiceNow stores it, but it IS stored.  This is something similar I used:

findGroups();
function findGroups(){
    var assignGroup = '';
    var getGroups = new GlideRecord('sys_variable_value');
    getGroups.addEncodedQuery('document!=wf_activity^ORdocument=NULL^document=sys_hub_action_instance^variableSTARTSWITHfiel^valueLIKEassignment_group=');
    getGroups.query();
    while(getGroups.next()){
        varSplit1 = getGroups.value.split(":");
        //varSplit2 = varSplit1[1].split(',');
        assignGroup = varSplit1[1];

        var getFlow = new GlideRecord('sys_hub_action_instance');
        getFlow.get(getGroups.document_key);
        gs.info('Flow: '+getFlow.flow.getDisplayValue()+" uses assignment group: "+assignGroup);
    }
}

 

Be sure to change the StartsWith query to a Contains if you can't guarantee it's the first field set.  

Geraldina Valer
Tera Contributor

Has anyone tried this? I have the same question. 

We need a script that, using an assignment group, returns all the flows where it is used. 

We queried the sys_hub_flow_component , sys_hub_action_instance_v2 , sys_variable_value tables, but were unable to obtain that data. 

thanks!