- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 11:04 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 12:21 PM - edited 07-17-2025 12:21 PM
@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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 11:36 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 11:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 12:21 PM - edited 07-17-2025 12:21 PM
@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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 09:25 PM
@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 !!