Need help with setting sys_updated_on for test data so records get purged
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi All,
I’m trying to generate test records in the problem table with backdated sys_updated_on values so that they fall under the purge rules (older than 4 months).
Here’s the script I’m using:
var startDate = new GlideDateTime('2025-07-14 10:00:00');
for (var i = 0; i < 10; i++) {
var gdt = new GlideDateTime(startDate);
gdt.addDaysLocalTime(i); // Add i days to start date
var gr = new GlideRecord('problem');
gr.initialize();
gr.short_description = 'Generated test problem ' + (i + 1);
gr.sys_updated_by = 'testuser';
// Insert first
var sys_id = gr.insert();
if (sys_id) {
var updateGr = new GlideRecord('problem');
if (updateGr.get(sys_id)) {
updateGr.autoSysFields(false);
updateGr.setWorkflow(false);
updateGr.setValue('sys_updated_on', gdt);
updateGr.setValue('sys_updated_by', 'testuser');
updateGr.update();
gs.info('Updated record: ' + sys_id + ' with date: ' + gdt);
}
}
}
The records are being created and sys_updated_on looks correct in the database, but they are not getting purged by our data retention job (which checks last updated date).
Has anyone faced this before?
Is overriding sys_updated_on not respected by purge rules?
Do I need to use a different approach to backdate records?
Any guidance would be appreciated.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Recommendation is not to change the value of system fields manually or via scripts.
Create a custom purge rule to delete specific records or do a manual cleanup rather than updating sys_updated_on or any of the system fields.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0859763
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0863440
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
my thoughts
-> If we override sys_updated_on it doesn't guarantee that the purge rule will consider those
-> to create test data why not use Import set, transform map
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