Fix script : Error on return in a function

Shree Nag
Tera Expert

Hello,

I'm trying to write a fix script to replace old groups with new names.

 

here is the script below. The script is erroring out on return. I'm unable to figure what is missing here.

Please help.

 

 

// Fix Script to update assignment groups for A Implementers across multiple tables
// Get the sys_id of the new group (A Support Group) 
 

var newGroupName = 'A Support Group';
var newGroupSysId = '';
var grNewGroup = new GlideRecord('sys_user_group');
grNewGroup.addQuery('name', newGroupName);
grNewGroup.query();
if (grNewGroup.next()) {
    newGroupSysId = grNewGroup.sys_id;
    gs.info('Found new group: ' + newGroupName + ' with Sys ID: ' + newGroupSysId);
} else {
    gs.error('New group "' + newGroupName + '" not found. Aborting script.');
    return;
}


// Get the sys_id of the old group (A Implementers)
// IMPORTANT: Replace 'A Implementers' with the *exact* name of your old group
var oldGroupName = 'A Implementers';
var oldGroupSysId = '';
var grOldGroup = new GlideRecord('sys_user_group');
grOldGroup.addQuery('name', oldGroupName);
grOldGroup.query();
if (grOldGroup.next()) {
    oldGroupSysId = grOldGroup.sys_id;
    gs.info('Found old group: ' + oldGroupName + ' with Sys ID: ' + oldGroupSysId);
} else {
    gs.error('Old group "' + oldGroupName + '" not found. Aborting script.');
     return;
}

// Array of tables to update
var tablesToUpdate = [
    'incident',          // Incidents
    'pm_project_task',   // Project Tasks
    'rm_story',          // Agile Stories
    'rm_scrum_task',     // Agile Scrum Tasks
    'sc_task',           // Catalog Tasks
   'change_request'      // Change request
    // Add any other relevant tables if needed, e.g., 'change_request', 'problem', etc.
];

// Loop through each table and update records
for (var i = 0; i < tablesToUpdate.length; i++) {
    var tableName = tablesToUpdate[i];
    gs.info('Processing table: ' + tableName);

    var gr = new GlideRecord(tableName);
    gr.addQuery('assignment_group', oldGroupSysId);
    // Optional: Add conditions to only update "open" or "active" tasks if desired
    // gr.addQuery('active', true);
    // gr.addQuery('state', '!=', '3'); // Assuming '3' is a closed state, adjust as needed

    gr.query();
    var updateCount = 0;
    while (gr.next()) {
        gr.assignment_group = newGroupSysId;
       
        // IMPORTANT: Consider if you want to prevent Business Rules and Workflow from running
        // This is generally recommended for bulk updates to avoid unintended side effects or notifications
        gr.setWorkflow(false);
        gr.autoSysFields(false); // Prevents 'Updated by' and 'Updated' fields from changing to admin user/timestamp

        gr.update();
        updateCount++;
    }
    gs.info('Updated ' + updateCount + ' records in table ' + tableName);
}
gs.info('Script execution complete.');


1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@Shree Nag You don't need return statement as its not a function. try below code. I have tested this for incident table only. You can add more table in "tablesToUpdate" array & replace group name.  Try to test this for one table then go for multiple tables.

var newGroupName = 'Software'; // replace your grpup name here
var oldGroupName = 'Hardware'; // replace your grpup name here

var newGroupSysId = getGroupSysID(newGroupName);
var oldGroupSysId = getGroupSysID(oldGroupName);

if (!gs.nil(newGroupSysId)) {

    if (!gs.nil(oldGroupSysId)) {
           
         // add more table in below array
        var tablesToUpdate = [
            'incident' // Incident
        ];

        // Loop through each table and update records
        for (var i = 0; i < tablesToUpdate.length; i++) {
            var tableName = tablesToUpdate[i];
            gs.info('Processing table: ' + tableName);

            var grTable = new GlideRecord(tableName);
            grTable.addQuery('assignment_group', oldGroupSysId);
            grTable.query();
            var updateCount = 0;
            while (grTable.next()) {
                gs.info('Processing table record: ' + grTable.getValue('number'));
                grTable.setValue('assignment_group', newGroupSysId, newGroupName);
                grTable.setWorkflow(false);
                grTable.autoSysFields(false);
                grTable.update();
                updateCount++;
            }
            gs.info('Updated ' + updateCount + ' records in table ' + tableName);
        }

    } else {
        gs.error('Old group "' + oldGroupName + '" not found. Aborting script.');
    }
} else {
    gs.error('New group "' + newGroupName + '" not found. Aborting script.');
}

gs.info('Script execution complete.');


function getGroupSysID(groupName) {
    if (gs.nil(groupName))
        return '';

    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', groupName);
    groupGR.setLimit(1);
    groupGR.query();
    if (!groupGR.next())
        return '';

    return groupGR.getUniqueValue();

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Which logs are you seeing? Do they contain the expected values?  Since you can't call a Fix Script from anything else, I'm not sure what 'return' is doing here, but at what point are you getting an error?

John Gilmore
Giga Guru

You don't provide enough information. What do you mean the script is erroring out on return? The way the script is provided above it doesn't return anything.

Also return within your .next() loop will break out of the entire script so if you want it to log the error and continue processing the rest of the script you will need to remove the return lines from the else section of your script.

SANDEEP28
Mega Sage

@Shree Nag You don't need return statement as its not a function. try below code. I have tested this for incident table only. You can add more table in "tablesToUpdate" array & replace group name.  Try to test this for one table then go for multiple tables.

var newGroupName = 'Software'; // replace your grpup name here
var oldGroupName = 'Hardware'; // replace your grpup name here

var newGroupSysId = getGroupSysID(newGroupName);
var oldGroupSysId = getGroupSysID(oldGroupName);

if (!gs.nil(newGroupSysId)) {

    if (!gs.nil(oldGroupSysId)) {
           
         // add more table in below array
        var tablesToUpdate = [
            'incident' // Incident
        ];

        // Loop through each table and update records
        for (var i = 0; i < tablesToUpdate.length; i++) {
            var tableName = tablesToUpdate[i];
            gs.info('Processing table: ' + tableName);

            var grTable = new GlideRecord(tableName);
            grTable.addQuery('assignment_group', oldGroupSysId);
            grTable.query();
            var updateCount = 0;
            while (grTable.next()) {
                gs.info('Processing table record: ' + grTable.getValue('number'));
                grTable.setValue('assignment_group', newGroupSysId, newGroupName);
                grTable.setWorkflow(false);
                grTable.autoSysFields(false);
                grTable.update();
                updateCount++;
            }
            gs.info('Updated ' + updateCount + ' records in table ' + tableName);
        }

    } else {
        gs.error('Old group "' + oldGroupName + '" not found. Aborting script.');
    }
} else {
    gs.error('New group "' + newGroupName + '" not found. Aborting script.');
}

gs.info('Script execution complete.');


function getGroupSysID(groupName) {
    if (gs.nil(groupName))
        return '';

    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', groupName);
    groupGR.setLimit(1);
    groupGR.query();
    if (!groupGR.next())
        return '';

    return groupGR.getUniqueValue();

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

SANDEEP28
Mega Sage

@Shree Nag Did you try my code. If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!