How to activate the user in user table(sys_user) by using background script

siri27
Tera Contributor

How to activate the user in user table(sys_user) by using background script

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @siri27 ,

Sample Script :-

 

// Query the sys_user table for all inactive users
var gr = new GlideRecord('sys_user');
gr.addQuery('active', false);
gr.query();

// Activate each user in the query result
while (gr.next()) {
    gr.active = true;
    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

Mohith Devatte
Tera Sage
Tera Sage

Hello @siri27 ,

you can try this script 

var gr = new GlideRecord('sys_user');
gr.addQuery('active', false);
gr.addQuery('sys_id','your_user_record_sys_id');
gr.query();
if (gr.next()) {
    gr.active = true;
    gr.update();
}

if there are multiple users you can use this script 

var gr = new GlideRecord('sys_user');
gr.addQuery('active', false);
gr.query();
while (gr.next()) {
    gr.active = true;
    gr.update();
}

Hope this helps 

Mark my answer correct if this helps you 

Thanks

 

 

Matt102
Giga Guru

Hi,

I'd echo the previous two suggestions, i.e. use a GildeRecord update, to set the sys_user.active field to true.

Just to be sure, you may also want to check/set the Locked Out field: sys_user.locked_out which should be set to  false, given that the user will be unable to log in if active == true and locked_out == true

HTH,matt