Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Rename attachment based on variable on record producer

vanshikasin
Tera Contributor

Screenshot 2025-10-25 124151.pngI have a Record Producer that includes a variable of type Attachment. My requirement is that whenever a user uploads a document through this attachment variable, the uploaded file should automatically be renamed based on the name of that specific variable.

Since my Record Producer contains multiple attachment variables, each with a different name, I want each uploaded file to be renamed according to its respective variable name.

1 ACCEPTED SOLUTION

mayankkumar
Kilo Patron
Kilo Patron

Hi @vanshikasin,
you can achieve it by having a onChange client script on your attachment variable and a script include for rename and validation
onChange Client script ->

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || !newValue) return;

	var attachmentSysId = newValue.toString();

	// var varName = control.name;
	var varName = 'variable_name';

	var checkGA = new GlideAjax('AttachmentVariableHelper');
	checkGA.addParam('sysparm_name', 'shouldRenameAttachment');
	checkGA.addParam('sysparm_varName', varName);
	checkGA.addParam('sysparm_asi', attachmentSysId);

	checkGA.getXMLAnswer(function (shouldRename) {
		
		if (shouldRename == 'true') {
			// Proceed only if rename is needed
			var ga = new GlideAjax('AttachmentVariableHelper');
			ga.addParam('sysparm_name', 'updateVariableName');
			ga.addParam('sysparm_varName', varName);
			ga.addParam('sysparm_asi', attachmentSysId);
			ga.getXMLAnswer(function (response) {
				g_form.clearValue('variable_name');
				g_form.setValue('variable_name', newValue);
			});
		} else {
			return;
		}
	});
}


Client Callable script include ->

var AttachmentVariableHelper = Class.create();
AttachmentVariableHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	shouldRenameAttachment: function () {
		var asi = this.getParameter('sysparm_asi'); // attachment sys_id
		var expectedName = this.getParameter('sysparm_varName'); // expected filename

		var att = new GlideRecord('sys_attachment');
		att.addQuery('sys_id', asi);
		att.query();
		if (att.next()) {
			var fileName = att.getValue('file_name');

			// Remove extension from actual file name (e.g., .pdf, .png)
			if (fileName.indexOf('.') > -1) {
				fileName = fileName.substring(0, fileName.lastIndexOf('.'));
			}

			// Compare base filename with expected variable name
			if (fileName.trim() == expectedName.trim()) {
				return 'false'; // No need to rename
			}
			return 'true'; // Different name — proceed
		}

		return 'true'; // if not found, default to true so rename can proceed
	},

	updateVariableName: function () {
		var varName = this.getParameter('sysparm_varName');
		var sid = this.getParameter('sysparm_asi');
		if (!varName) return 'Missing variable name';

		var att = new GlideRecord('sys_attachment');
		att.addQuery('sys_id', sid);
		att.query();

		if (att.next()) {
			var originalFileName = att.getValue('file_name');
			// Get file extension
			var fileExtension = '';
			if (originalFileName.indexOf('.') > -1) {
				var parts = originalFileName.split('.');
				fileExtension = '.' + parts[parts.length - 1];
			}
			var newFileName = varName + fileExtension;
			att.file_name = newFileName;
			att.update();
			return 'Attachment updated with variable name ' + varName;
		}

		return 'No attachment found for tagging';
	}

});


This approach updates the backend file name of the uploaded attachment and reflects the change on the record producer form as well.
However, you’ll need to configure an onChange client script separately for each attachment variable if you want this behavior across all attachment fields.

Please give it a try and let me know if it works for you.
This may not be the only way to achieve the desired result, if you or anyone else has a more efficient approach, I’d love to hear it.
--------------------------------------------------------------------------------------------------------------------------------------
Please mark my response helpful and accept as solution
Thanks & Regards
Mayank
Rising Star 2025

View solution in original post

4 REPLIES 4

Abbas_5
Tera Sage
Tera Sage

Hello @vanshikasin,

Please refer to the link below:
https://www.servicenow.com/community/developer-forum/rename-attachment-during-upload-in-an-record-pr...

 

If it is helpful, please hit the thumbs-up button and accept the correct solution by referring to this solution in the future. It will be helpful to them.

 

Thanks & Regards,

Abbas Shaik

Ankur Bawiskar
Tera Patron
Tera Patron

@vanshikasin 

what's the requirement of renaming?

you can rename those after record producer is submitted.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

mayankkumar
Kilo Patron
Kilo Patron

Hi @vanshikasin,
you can achieve it by having a onChange client script on your attachment variable and a script include for rename and validation
onChange Client script ->

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || !newValue) return;

	var attachmentSysId = newValue.toString();

	// var varName = control.name;
	var varName = 'variable_name';

	var checkGA = new GlideAjax('AttachmentVariableHelper');
	checkGA.addParam('sysparm_name', 'shouldRenameAttachment');
	checkGA.addParam('sysparm_varName', varName);
	checkGA.addParam('sysparm_asi', attachmentSysId);

	checkGA.getXMLAnswer(function (shouldRename) {
		
		if (shouldRename == 'true') {
			// Proceed only if rename is needed
			var ga = new GlideAjax('AttachmentVariableHelper');
			ga.addParam('sysparm_name', 'updateVariableName');
			ga.addParam('sysparm_varName', varName);
			ga.addParam('sysparm_asi', attachmentSysId);
			ga.getXMLAnswer(function (response) {
				g_form.clearValue('variable_name');
				g_form.setValue('variable_name', newValue);
			});
		} else {
			return;
		}
	});
}


Client Callable script include ->

var AttachmentVariableHelper = Class.create();
AttachmentVariableHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	shouldRenameAttachment: function () {
		var asi = this.getParameter('sysparm_asi'); // attachment sys_id
		var expectedName = this.getParameter('sysparm_varName'); // expected filename

		var att = new GlideRecord('sys_attachment');
		att.addQuery('sys_id', asi);
		att.query();
		if (att.next()) {
			var fileName = att.getValue('file_name');

			// Remove extension from actual file name (e.g., .pdf, .png)
			if (fileName.indexOf('.') > -1) {
				fileName = fileName.substring(0, fileName.lastIndexOf('.'));
			}

			// Compare base filename with expected variable name
			if (fileName.trim() == expectedName.trim()) {
				return 'false'; // No need to rename
			}
			return 'true'; // Different name — proceed
		}

		return 'true'; // if not found, default to true so rename can proceed
	},

	updateVariableName: function () {
		var varName = this.getParameter('sysparm_varName');
		var sid = this.getParameter('sysparm_asi');
		if (!varName) return 'Missing variable name';

		var att = new GlideRecord('sys_attachment');
		att.addQuery('sys_id', sid);
		att.query();

		if (att.next()) {
			var originalFileName = att.getValue('file_name');
			// Get file extension
			var fileExtension = '';
			if (originalFileName.indexOf('.') > -1) {
				var parts = originalFileName.split('.');
				fileExtension = '.' + parts[parts.length - 1];
			}
			var newFileName = varName + fileExtension;
			att.file_name = newFileName;
			att.update();
			return 'Attachment updated with variable name ' + varName;
		}

		return 'No attachment found for tagging';
	}

});


This approach updates the backend file name of the uploaded attachment and reflects the change on the record producer form as well.
However, you’ll need to configure an onChange client script separately for each attachment variable if you want this behavior across all attachment fields.

Please give it a try and let me know if it works for you.
This may not be the only way to achieve the desired result, if you or anyone else has a more efficient approach, I’d love to hear it.
--------------------------------------------------------------------------------------------------------------------------------------
Please mark my response helpful and accept as solution
Thanks & Regards
Mayank
Rising Star 2025

mayankkumar
Kilo Patron
Kilo Patron

Hi @vanshikasin,

Did you get the chance to try this solution?

If you have any queries, feel free to ask

----------------------------------------------------

Please mark my response helpful and accept as solution
Thanks & Regards
Mayank