Adding multiple values in list type of field using fix script

Community Alums
Not applicable

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 )

 

group1.png

 

 

example 2)
If the Admin Support group has no value in the type field, I want to add it to Knowledge."

 

group 2.png

 

17 REPLIES 17

HIROSHI SATOH
Mega Sage

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
}

 

Community Alums
Not applicable

HI @HIROSHI SATOH 

"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."

 

solution 3.png

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
}

Community Alums
Not applicable

Hi @HIROSHI SATOH  Facing Same issue it adds the Knowledge type twice