- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 09:32 AM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 10:29 AM
If you do go with deactivating, rather than deleting, I'd do it through the UI:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2015 07:25 AM
Yeah, autoSysFields(false); would be for insert/update. Not really applicable for deletion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2015 07:31 AM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2015 07:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 10:14 AM
Have you checked for records that will have orphaned references?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 10:19 AM
Hmm good point - the concept of deleting users is not good - perhaps setting to locked out and active = false might be preferable