Rename existing attachments belonging to a specific table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 08:01 AM - edited 04-25-2024 08:10 AM
Hello Community,
I would like to rename all existing attachments related to their respective incidents following this nomonclature: [Incident_number]_[FileName].extension.
I have already created a view to link the sys_attachment table and the incidents table. However, I'm not sure how to proceed next. Should I create a script that runs as long as there are files or BR to handle this requirement?
Any help is much appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 11:02 AM
Hi Edgar-d,
You can test the following script logic in Scripts Background
var att = new GlideRecord('sys_attachment');
att.addQuery('table_name', 'incident');
att.query();
gs.info("Found " + att.getRowCount() + " sys_attachment record for the incident table");
while (att.next()) {
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', att.table_sys_id);
inc.query();
if (inc.next()) {
var newFileName = inc.number + "_" + att.file_name;
gs.info("file_name = " + att.file_name + ", new file_name = " + newFileName);
// att.file_name = newFileName;
// att.update();
}
else {
gs.info("Didn't find incident record with sys_id = " + att.table_sys_id);
}
}
(I will say that I get some odd java error:
Unparseable date: "5": java.text.ParseException: Unparseable date: "5": java.base/java.text.DateFormat.parse(DateFormat.java:395)
when I run it in my PDI. But doesn't seem to affect the script. I got the desired result on the file_name when I un-commented the two lines that set the field value and updates the record.)
You *could* use similar logic in a BR defined on the sys_attachment table, I'm not sure how that would affect performance. But that is easy enough to test.