Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script to change group names

mariasilva
Tera Contributor

Hello everyone, I need to remove the word SQUAD or squad, from the beginning of the name of 41 groups in the ServiceNow production environment. I need to run it through a fix script to register the change, but I have never used a fix script. Could someone help me, please?

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@mariasilva Here is the script which looks for the keyword Squad at the beginning of the group name and updates it.

 

var gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery('nameSTARTSWITHSQUAD^ORnameSTARTSWITHsquad'); // Case-insensitive check
gr.query();

while (gr.next()) {
    var originalName = gr.name.toString();
    var newName = originalName.replace(/^squad\s+/i, ''); // Removes "squad " or "SQUAD " from the start

    if (newName !== originalName) {
        gs.info('Renaming group: ' + originalName + ' -> ' + newName);
        gr.name = newName;
        gr.update();
    }
}

Caution: Although I tested this script and it works as intended but I recommend you to run this script in a sandbox instance first to verify the changes.