Adding multiple values in list type of field using fix script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2024 06:58 AM
I have a requirement for my project: I need to update 350 records in the "user group" table. Specifically, there is a field called "Type" (which is a list-type field) that needs to be updated. For each record, I want to add the value "knowledge" to the existing values in the "Type" field, without removing the existing values.
For example, 1)
if the "Global Service Desk" group has the "ITIL" value in the "Type" field, I want to update it to include both "ITIL" and "knowledge," so the final value should be "ITIL, knowledge." (By using fix script I need to complete this requirement )
example 2)
If the Admin Support group has no value in the type field, I want to add it to Knowledge."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2024 10:33 AM
This script will iterate over all the records in the sys_user_group table and update the type field accordingly.
var groupRecord = new GlideRecord('sys_user_group'); // Group table
groupRecord.query();
while (groupRecord.next()) {
var currentType = groupRecord.getValue('type'); // Get the current value of the 'type' field
if (currentType) {
if (!currentType.includes('knowledge')) { // Check if 'knowledge' is already present
groupRecord.setValue('type', currentType + ',' + 'knowledge');
}
} else {
groupRecord.setValue('type', 'knowledge'); // Set the 'type' field to 'knowledge' if it was empty
}
groupRecord.update(); // Save the record
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:12 PM
"I’m facing an issue with this script: if the Knowledge type is already present, the script doesn’t work as required. It should not add the Knowledge type again if it's already there, but according to the script you provided, it adds the Knowledge type twice."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:37 PM
It's a problem with includes. How about this?
var groupRecord = new GlideRecord('sys_user_group'); // Group table
groupRecord.query();
while (groupRecord.next()) {
var currentType = groupRecord.getValue('type'); // Get the current value of the 'type' field
if (currentType) {
var typesArray = currentType.split(','); // Split the field into an array
if (!typesArray.includes('knowledge')) { // Check if 'knowledge' is already in the array
typesArray.push('knowledge'); // Add 'knowledge' to the array
groupRecord.setValue('type', typesArray.join(',')); // Join the array back into a string and set the field
}
} else {
groupRecord.setValue('type', 'knowledge'); // Set the 'type' field to 'knowledge' if it was empty
}
groupRecord.update(); // Save the record
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:43 PM
Hi @HIROSHI SATOH Facing Same issue it adds the Knowledge type twice