The CreatorCon Call for Content is officially open! Get started here.

Script used to delete some specific users from snow

geet
Tera Guru

Greetings All,

I have written a script to delete those users whose Email, User ID, Ldap-Server is blank. Please let me know if it is ok.

var encodedQuery="emailISEMPTY^user_nameISEMPTY^ldap_serverISEMPTY";   // semicolon added

var gr = new GlideRecord("sys_user");     //error here " needed to be closed

gr.addEncodedQuery(encodedQuery);

gr.deleteMultiple();

1 ACCEPTED SOLUTION

If you do go with deactivating, rather than deleting, I'd do it through the UI:


Capture.PNG


Capture.PNG


View solution in original post

22 REPLIES 22

Yeah, autoSysFields(false); would be for insert/update.   Not really applicable for deletion.


ismailsn
ServiceNow Employee
ServiceNow Employee

Exactly. What should be done is just get a list of records that were requested by/requested for these users that you will be deleting before you do so.



This can be done doing the following:



deleteUser();



function deleteUsers(){




  var target = new GlideRecord('sys_user');


  target.addNullQuery('user_name');


  target.addNullQuery('email');


  target.addNullQuery('ldap_server');


  target.query();


  while(target.next()){



  var taskRecords = new GlideRecord('task');


  taskRecords.addQuery('requested_for', target.sys_id );


  taskRecords.query();


  while(taskRecords.next()){




  gs.print("The record " + taskRecords.number + " is associated with sys_user record" + target.sys_id);




  }




  //Uncomment the following line when you want to commit the delete


  //target.deleteRecord();



  }




}


requested_for is not a task field.   Its on sc_request and may be a few others.   That's what makes deletion of user accounts so flipping dangerous.   Each task type typically has its own user reference to mean "consumer".   That's before you start worrying about tickets that were assigned, group memberships, delegation, etc.


Uncle Rob
Kilo Patron

Have you checked for records that will have orphaned references?


Hmm good point - the concept of deleting users is not good - perhaps setting to locked out and active = false might be preferable