Script to add types to another group

AlejandroE
Tera Contributor

Hey community,

 

We are facing a problem with an script we are setting up to update nealty 200 groups with new types.

 

The fact is that is working but is not skipping the groups that had this type assigned and is assigning them again and again.

 

I have part of the script which is the one that is including the types but we don't find the issue... 

 

Could you please support on that? Thank you so much!!

 

PS. ignore the comment lines hahaha.

2 REPLIES 2

Bhavya11
Kilo Patron

Hi @AlejandroE ,

 

 

could please try below script 

var newTypes = ["problem", "normal"];

var groupsList = [];

// Fetch unique Assignment Groups from the Problem table
var problemGR = new GlideRecord('problem');
problemGR.query();
while (problemGR.next()) {
    var groupName = problemGR.assignment_group.name; // Use the 'name' field directly
    if (groupName && groupsList.indexOf(groupName) === -1) {
        groupsList.push(groupName);
    }
}

// Update the groups in the sys_user_group table
groupsList.forEach(function(groupName) {
    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', groupName);
    groupGR.query();

    if (groupGR.next()) {
        // Split existing types into an array, trimming spaces
        var currentTypes = (groupGR.type || "").split(',').map(function(type) {
            return type.trim();
        });

        // Identify types to add by excluding already existing ones
        var typesToAdd = newTypes.filter(function(newType) {
            return currentTypes.indexOf(newType) === -1;
        });

        // If there are new types to add, update the record
        if (typesToAdd.length > 0) {
            currentTypes = currentTypes.concat(typesToAdd); // Add only new types
            groupGR.type = currentTypes.join(','); // Update the 'type' field
            groupGR.update(); // Save the changes

            gs.print("Updated group '" + groupName + "' with new types: " + typesToAdd.join(', '));
        } else {
            gs.print("Skipped group '" + groupName + "' as it already has all the required types.");
        }
    } else {
        gs.print("Group not found: " + groupName);
    }
});

 

 

 

Please mark my solution as Accept, If you find it helpful.

 

Thanks,

BK

Hey, thanks for the answer.

 

We've tried and still adding duplicates.

 

It's a nightmare this script, honestly hahaha.

 

If you have any idea it will be great, btw I'll continue looking for solutions.

 

Thanks for everything.