Attachment version renaming

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2024 10:04 PM
While uploading a attachment in submission record if a attachment is already there and a same named attachement is getting attached it should rename the new attached document as a version.
For example Test.txt is there again if i attach Test.txt it should get attached and renamed as Test_V1.txt
so now my record should have 2 attachment records as Test.txt,Test_V1.txt
@Ankur Bawiskar Can you help in this.
Regards,
Debasis

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2024 11:21 PM
Hello @Ankur Bawiskar can you help on this please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2024 11:51 PM
you can use before insert BR on sys_attachment table
Something like this; tweak it as per your requirement and check by adding logs to debug if it doesn't work directly
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var fileName = current.file_name;
var lastIndexOf = fileName.lastIndexOf('.');
var fileExtension = fileName.substring(lastIndexOf + 1, fileName.length);
var attFileName = fileName.substring(0, lastIndexOf);
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", current.table_sys_id);
gr.addQuery("file_name", current.file_name);
gr.query();
var count = gr.getRowCount();
// get the total count for same file so that you can update the version
if(count > 0){
count++;
attFileName = attFileName + "_V" + count;
}
current.file_name = attFileName + "." + fileExtension;
})(current, previous);
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-19-2024 12:19 AM
Hello Ankur when i tried this script getting the below error"Access to api 'put(sys_attachment.file_name)' from scope 'x_htms5_submissi_0' has been refused due to the api's cross-scope access policy."
and the same file name is getting attached.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 01:44 AM
Hello @Ankur Bawiskar getting the "Access to api 'put(sys_attachment.file_name)' from scope 'x_htms5"has been refused due to the api's cross-scope access policy."
How to fix this issue?
This is cross appplication issue.
Regards,
Debasis

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 01:55 AM
i have created
Still this error is there @Ankur Bawiskar if you can provide the right direction.
Regards,
Debasis