- 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 10:33 AM
did you check if your query is giving correct count?
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();
gs.info(hrObj.getRowCount());
hrObj.deleteMultiple();
} catch (error) {
// Handle the error if something goes wrong
gs.error("Error cleaning up HR profiles: " + error.message);
}
}
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 07:36 PM
you are trying to delete HR profile but from which scope?
Global scope?
if yes then you should get cross scope issue for delete
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 01:38 PM - edited 04-10-2025 01:39 PM
If you want to log which records are deleted, then add something like:
gs.info('cleanUpContingentHRProfiles script: deleting profile for user: ' + hrObj.user);
In the 'While()' loop before the delete() in Brad's script. the you can use the Script Log Statements module and add a filter condition: "Message", "start withs", "cleanUpContigentHRProfiles: ".

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 06:17 PM
@si21 This could be related to cross application scope access? Either create the schedule job in the HR scope or check if there any Application restricted caller access records are in (Requested/Invalidated/No allowed) state and set them allowed to let the script execute.
Also, instead of doing this deletion via a script, you can choose to use a Delete job https://www.servicenow.com/docs/bundle/yokohama-platform-administration/page/administer/managing-dat... to delete such records.