Remove existing user from Glide List field

Rajareddy_VR
Tera Contributor

Team,

 

I have a requirement to remove particular existing user from glide list field.

Table: Entity (sn_grc_profile)

field: u_service_manager

 

I have created fix script to remove existing users from the field but not deleting exact user with sysid.

I have to remove only Abel tutar or abraham lincoln

 

Could you please help me to achieve this?

 

Fix script:

 

var encodedquery = 'nameSTARTSWITHTest_090601^u_service_managerLIKE62826bf03710200044e0bfc8bcbe5df1^ORu_service_managerLIKEa8f98bb0eb32010045e1a5115206fe3a';
 
var gr = new GlideRecord('sn_grc_profile');
gr.addEncodedQuery(encodedquery);
gr.query();
while (gr.next()) {
    var arr = gr.u_service_manager.toString().split(','); // convert to array
   var pos = arr.indexOf('62826bf03710200044e0bfc8bcbe5df1');
arr.splice(pos, 1);
    gr.u_service_manager = arr.toString();
gr.update();
}

 

RajareddyIBM_0-1693119262974.png

@Harneet Sital @Ankur Bawiskar @Jaspal Singh 

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Rajareddy_VR ,

 

You can give a try on below code :

var encodedquery = 'nameSTARTSWITHTest_090601^u_service_managerLIKE62826bf03710200044e0bfc8bcbe5df1^ORu_service_managerLIKEa8f98bb0eb32010045e1a5115206fe3a';
 
var gr = new GlideRecord('sn_grc_profile');
gr.addEncodedQuery(encodedquery);
gr.query();

var userToRemove1 = '62826bf03710200044e0bfc8bcbe5df1'; // Abel Sys id
var userToRemove2 = 'a8f98bb0eb32010045e1a5115206fe3a'; // Abraham Sys id

while (gr.next()) {
    var arr = gr.u_service_manager.toString().split(','); 
    var updatedArr = []; 

    for (var i = 0; i < arr.length; i++) {
        var currentUser = arr[i].trim(); 

        
        if (currentUser !== userToRemove1 && currentUser !== userToRemove2) {
            updatedArr.push(currentUser); 
        }
    }

    gr.u_service_manager = updatedArr.join(', '); 
    gr.update();
}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

3 REPLIES 3

Samaksh Wani
Giga Sage
Giga Sage

Hello @Rajareddy_VR 

 

Can you tell does the user is not deleting or wrong user is deleting with ur above script.

 

 

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Rajareddy_VR ,

 

You can give a try on below code :

var encodedquery = 'nameSTARTSWITHTest_090601^u_service_managerLIKE62826bf03710200044e0bfc8bcbe5df1^ORu_service_managerLIKEa8f98bb0eb32010045e1a5115206fe3a';
 
var gr = new GlideRecord('sn_grc_profile');
gr.addEncodedQuery(encodedquery);
gr.query();

var userToRemove1 = '62826bf03710200044e0bfc8bcbe5df1'; // Abel Sys id
var userToRemove2 = 'a8f98bb0eb32010045e1a5115206fe3a'; // Abraham Sys id

while (gr.next()) {
    var arr = gr.u_service_manager.toString().split(','); 
    var updatedArr = []; 

    for (var i = 0; i < arr.length; i++) {
        var currentUser = arr[i].trim(); 

        
        if (currentUser !== userToRemove1 && currentUser !== userToRemove2) {
            updatedArr.push(currentUser); 
        }
    }

    gr.u_service_manager = updatedArr.join(', '); 
    gr.update();
}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

ShafrazMubarak
Giga Guru

The fix script you have provided is correct, but it is only removing the user with the sys_id 62826bf03710200044e0bfc8bcbe5df1. To remove the users Abel Tutar and Abraham Lincoln, you can make the following changes to the script:

  1. Change the encoded query to:

var encodedquery = 'nameSTARTSWITHTest_090601^u_service_managerLIKE"Abel Tutar"^ORu_service_managerLIKE"Abraham Lincoln"';

This will match any records where the name field starts with Test_090601 and the u_service_manager field contains the string Abel Tutar or Abraham Lincoln. 2. Inside the while loop, add the following code to check if the current record contains the user you want to remove:

var user = gr.u_service_manager.toString().split(',')[0];
if (user == 'Abel Tutar' || user == 'Abraham Lincoln') {
  // Remove the user from the list.
  var arr = gr.u_service_manager.toString().split(',');
  arr.splice(0, 1);
  gr.u_service_manager = arr.toString();
}

This code will check the first element of the u_service_manager array. If the element is equal to Abel Tutar or Abraham Lincoln, then the user will be removed from the list.

The updated fix script would look like this:

var encodedquery = 'nameSTARTSWITHTest_090601^u_service_managerLIKE"Abel Tutar"^ORu_service_managerLIKE"Abraham Lincoln"';

var gr = new GlideRecord('sn_grc_profile'); gr.addEncodedQuery(encodedquery); gr.query();

while (gr.next()) { var user = gr.u_service_manager.toString().split(',')[0]; if (user == 'Abel Tutar' || user == 'Abraham Lincoln') { // Remove the user from the list. var arr = gr.u_service_manager.toString().split(','); arr.splice(0, 1); gr.u_service_manager = arr.toString(); }

gr.update(); }

I hope this helps! Let me know if you have any other questions.