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.

Server side scripting portal widget snow

DB1
Tera Contributor

Hello All,

 

I have the following requirement:

1. Deactivate the user selected from list of records.

2. Also, delete all the relationship that the user has

3. Condition -> current logged in User should not deactivate him/herself

 

I am trying to do it via Server side scripting but for some reason I do not get it right, need help on the same

 


data.user = gs.getUserID();
if(input&&input.username)
{

var grUserdel = new GlideRecord('sys_user');
grUserdel.addQuery('sys_id',input.username);
grUserdel.query();
if(grUserdel.next())
{

if(data.user == input.username)
{
gs.addInfoMessage("Logged in User cannot deactivate themselves");
return false;
}
else
{

var userid =grUserdel.sys_id;
grUserdel.active="false";
gs.addInfoMessage("User " +grUserdel.name +" has been Deactivated");


var grUserrel1 = new GlideRecord('cmdb_rel_person');
grUserrel1.addQuery('user',userid);
grUserrel1.query();
while(grUserrel1.next())
{
grUserrel1.deleteMultiple();
}
}

grUserdel.update();
}

}

Experts please need your suggestion here

 

@Ankur Bawiskar @Saurav9 @Amit Gujarathi @Mark Roethof @AnubhavRitolia @Pavankumar_1 @Murthy Ch @asifnoor @Anurag Tripathi 

7 REPLIES 7

Hey Anubhav,

I get this from the console log after stringify

 

{userid: '6816f79cc0a8016401c5a33be04be441,6816f79cc0a8016401c5a33be04be441',

Hi @DB1 

 

{userid: '6816f79cc0a8016401c5a33be04be441,6816f79cc0a8016401c5a33be04be441'}

 

Is this your value of input.username?

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

BigZolo
Tera Expert

First of all, it's confusing that the input is input.username and you are querying by sys_id. If the input is really the username, you should use grUserdel.get('user_name', input.username);

Also i would put the check if the current user is same as the input before the GlideRecord query.