Error on Fix Script

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

Chaitanya ILCR
Kilo Patron

Hi @Shree Nag ,

(function() {

    // 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');
    if (grNewGroup.get('name', newGroupName)) {
        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');
    if (grOldGroup.get('name', oldGroupName)) {
        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.');
})();

 

 

try this

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

3 REPLIES 3

SANDEEP28
Mega Sage

@Shree Nag Yesterday you posted same question in another thread. I gave you new code as well in below post. Did you try that code?

 

Why are you posting same question again

 

https://www.servicenow.com/community/developer-forum/fix-script-error-on-return-in-a-function/m-p/33...

Chaitanya ILCR
Kilo Patron

Hi @Shree Nag ,

(function() {

    // 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');
    if (grNewGroup.get('name', newGroupName)) {
        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');
    if (grOldGroup.get('name', oldGroupName)) {
        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.');
})();

 

 

try this

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Nishant8
Giga Sage

Hello @Shree Nag , you are using return without wrapping your code inside any function. Either wrap entire code inside a function or remove the return statement.

 

Regards,

Nishant