We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Attachment version renaming

Debasis Pati
Kilo Sage

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

14 REPLIES 14

Hello @Ankur Bawiskar  can you help on this please

@Debasis Pati 

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

 

AnkurBawiskar_0-1713509213297.png

(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.

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

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.

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

i have created 

DebasisPati_0-1713516881310.png

Still this error is there  @Ankur Bawiskar if you can provide the right direction.

Regards,
Debasis