- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I 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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
