GlideRecord deleteRecord() won't delete the record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2020 02:57 PM
I'm trying to remove from SN bad records loaded previously from Workday. For example, if someone is hired in error and then that hire is rescinded, I want to remove the EE from ServiceNow. I can tell it's a rescinded hire because the Workday Web Services response returns an empty Original_Hire_Date. So I put the following in the User table Transform Map's onStart:
var stagingGR = new GlideRecord('sn_hr_integrations_worker_profile');
stagingGR.addQuery('sys_import_state', 'pending');
stagingGR.addNullQuery('u_original_hire_date');
stagingGR.query();
while (stagingGR.next()) {
debug("Staging table record " + stagingGR.sys_id + " for worker " + stagingGR.u_worker_id);
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', stagingGR.u_worker_id);
userGR.query();
while (userGR.next()) {
debug("User record: " + userGR.sys_id + " for user: " + userGR.user_name + ", " + userGR.name);
debug("Can delete? " + userGR.canDelete());
//var deleted = userGR.deleteRecord();
debug("Deleted? " + userGR.deleteRecord());
if (userGR)
debug("Record is still there. Delete failed.");
else debug("Worker with userID " + stagingGR.u_worker_id + " has no original hire date so I deleted their record from SN.");
}
}
(debug is just a helper function that prepends a timestamp in milliseconds to the log entry.)
In the App Log, I see:
2020-03-09 17:14:41
13m ago
Information [1583788481790] Staging table record d5a1d2881bab041400af33fccd4bcbaa for worker 123456
2020-03-09 17:14:41
13m ago
Information [1583788481798] User record: f6a8ef5a1b5bc8d000af33fccd4bcb4d for user: 123456, John Doe
2020-03-09 17:14:41
13m ago
Information [1583788481800] Can delete? true
2020-03-09 17:14:41
13m ago
Information [1583788481803] Deleted? false
2020-03-09 17:14:41
13m ago
Information [1583788481806] Record is still there. Delete failed.
What am I missing?
- The record is found and I'm allowed to delete it (since sys_user.user_name is a coalesce field, I tried it with userGR.get() before resorting to while (userGR.next()) and the result is the same).
- There's no error thrown when I try to delete (I previously put all of the userGR logic into a try{} block but no error was ever caught).
- The record is still there.
Any ideas why?
Thanks in advance!
- Labels:
-
HR Service Delivery

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2020 03:03 PM
Never delete a user.
Set them to sys_user.active = false
sys_user.locked_out will then be set to true.
There is no need to delete a user.
But otherwise try;
userGR.setWorkflow(false)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2020 05:51 AM
Agree with you in principle but:
- SN is not the system of record for users - Workday is (Workday Worker ID = SN sys_user.user_name)
- They represent incorrect duplicate records of existing users so leaving them in (even Inactive) generates confusion for our SN users
These are not actual users and they will never appear again. Their unique Workday UserIDs will not be recycled and once their hires are rescinded in Workday (the system of record for all users imported into SN) they really do not exist.
If I can't whack them programmatically in the integration, I'll still be deleting them by hand later.
The solution is to stop hiring people in error in the first place but that's well out of my control <sigh>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2020 03:04 PM
Can we try??
var stagingGR = new GlideRecord('sn_hr_integrations_worker_profile');
stagingGR.addQuery('sys_import_state', 'pending');
stagingGR.addNullQuery('u_original_hire_date');
stagingGR.query();
while (stagingGR.next()) {
debug("Staging table record " + stagingGR.sys_id + " for worker " + stagingGR.u_worker_id);
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', stagingGR.u_worker_id);
userGR.query();
while (userGR.next()) {
debug("User record: " + userGR.sys_id + " for user: " + userGR.user_name + ", " + userGR.name);
debug("Can delete? " + userGR.canDelete());
//var deleted = userGR.deleteRecord();
userGR.deleteRecord();
debug("Deleted? " + userGR.deleteRecord());
if (userGR)
debug("Record is still there. Delete failed.");
else debug("Worker with userID " + stagingGR.u_worker_id + " has no original hire date so I deleted their record from SN.");
}
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2020 05:53 AM
Thanks Prateek,
I did. Before I tried assigning a a variable to userGR.deleteRecord() so I could easily log the result, I just called it by itself on its own line, and got the same result.