- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:09 AM
Hi experts,
We have below scheduled job created to run daily and when we click on execute now manually, none of the expected records getting deleted.
Please guide me what went wrong
function cleanUpContingentHRProfiles() {
try {
// Cleanup all HR profiles created for Contingent workers
var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
var hrObj = new GlideRecord("sn_hr_core_profile");
hrObj.addEncodedQuery(encodedstr1);
hrObj.deleteMultiple();
} catch (error) {
// Handle the error if something goes wrong
gs.error("Error cleaning up HR profiles: " + error.message);
}
}
TIA
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:26 AM
you forgot to invoke that function if the above script is the actual script
add this line as well to actually query
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:13 AM
You're not actually executing the instantiation of the GlideRecord, so not returning any records. It's usually better to use deleteRecord instead of Multiple
function cleanUpContingentHRProfiles() {
try {
// Cleanup all HR profiles created for Contingent workers
var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
var hrObj = new GlideRecord("sn_hr_core_profile");
hrObj.addEncodedQuery(encodedstr1);
hrObj.query();
while (hrObj.next()) {
hrObj.deleteRecord();
}
} catch (error) {
// Handle the error if something goes wrong
gs.error("Error cleaning up HR profiles: " + error.message);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:26 AM
you forgot to invoke that function if the above script is the actual script
add this line as well to actually query
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:35 AM
Hi @si21,
Please use:
cleanUpContingentHRProfiles();
function cleanUpContingentHRProfiles() {
try {
// Cleanup all HR profiles created for Contingent workers
var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
var hrObj = new GlideRecord("sn_hr_core_profile");
hrObj.addEncodedQuery(encodedstr1);
hrObj.query();
hrObj.deleteMultiple();
} catch (error) {
// Handle the error if something goes wrong
gs.error("Error cleaning up HR profiles: " + error.message);
}
}
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 08:46 AM - edited 04-10-2025 10:23 AM
HI @Ankur Bawiskar , thanks for the quick reply, I tried the code you have provided but still none of records got deleted in dev.
Is it because I have created this job in Global scope and action is on HR table?
I have also created the same script in HR scope and it worked well.
Also can we know which HR profiles deleted in logs if we need to check?