Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Need help with setting sys_updated_on for test data so records get purged

TEJAS
Tera Contributor

 

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.

2 REPLIES 2

Bhuvan
Giga Patron

@TEJAS  

 

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://www.servicenow.com/community/platform-analytics-forum/what-triggers-the-sys-updated-on-field...

 

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

Ankur Bawiskar
Tera Patron

@TEJAS  

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader