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.

Remove values from existing groups

Revathi12
Tera Contributor

Hi Team,

 

I have 2 fields, Existing groups and remove existing groups. In existing groups the values are populated based on the user groups, and in Remove existing groups I wanted  to remove those which are in existing groups field. I have tried with below on change client script.

 

 var eg = g_form.getValue('groups'); // groups is the field name of existing groups
   alert("Existing Groups" + eg);

   var re = g_form.getValue('remove_ex'); // remove_ex is the field name of remove existing groups
   alert("Remove Existing groups" + re);

   var egarr = eg.split(',');
   var rearr = re.split(',');
    alert('Remove array' ,+rearr);

   var searchStr = rearr[rearr.length-1];
    alert('Search String' ,+searchStr);
if (eg.indexOf(searchStr) < 0) {

    rearr = rearr.slice(0, -1);
}
alert("Sliced " + rearr);
if (re != rearr.toString()) {
    g_form.setValue('remove_ex', rearr.toString());
}


 }
1 REPLY 1

_Gaurav
Kilo Sage

Hi @Revathi12 

Use the below code and modify it accordingly

var existing_group = [101,102,103,104,105];
var remove_existing_group = [101,201,202,203,204];

for( var i = 0; i < remove_existing_group .length; i++){
for(var j =0 ;j<existing_group .length;j++)
{
    if ( remove_existing_group [i] === existing_group [j]) {
        remove_existing_group .splice(i, 1);
    }
}
}
gs.info(arr.toString());

OUTPUT
201,202,203,204

So the above code removes the common sys_id in these 2 arrays and prints the unique sys_ids

Please mark this as helpful and accept it as a solution if this resolves your query.
Thanks!