- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2025 10:53 AM
Hi Team,
Can we restrict special characters (% $ @ ( ) & < > ' ") in the attachment type variable?
@Ankur Bawiskar
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2025 10:11 PM
This is the working solution which worked for me fine
onChange catalog client script on Attachment type variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('variableName');
var ga = new GlideAjax('ValidateAttachmentFileName');
ga.addParam('sysparm_name', 'checkFileName');
ga.addParam('sysparam_attSysId', newValue);
ga.getXMLAnswer(function(answer) {
if (answer.toString() == 'invalid') {
g_form.showFieldMsg('variableName', 'Invalid file name', 'error');
}
});
}
Script Include: It should be client callable
var ValidateAttachmentFileName = Class.create();
ValidateAttachmentFileName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkFileName: function() {
var badChars = /[%$@()&<>'"]/;
var sysId = this.getParameter('sysparam_attSysId');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("sys_id", sysId);
gr.query();
if (gr.next()) {
if (badChars.test(gr.file_name.toString()))
return 'invalid';
else
return 'valid';
}
},
type: 'ValidateAttachmentFileName'
});
Output: Working fine
💡 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
12-02-2025 02:46 AM - edited 12-02-2025 03:02 AM
Hi @Sarthak Kashyap
Thanks for your response. I checked your solution, but it is still not working for me. The system is still allowing files with special characters in the name to be attached. Please check the attached screenshot.
FYI, I have also used the variable attribute 'allowed_extensions=csv' for this attachment type variable.
Please check the below client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2025 08:51 AM
Hi @D V Sandeep , Did you check the console what error it is show ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2025 09:24 PM
why are you concerned about file name in attachment type variable?
you can write onChange catalog client script on that Attachment type variable and use GlideAjax to validate it
💡 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
12-02-2025 10:11 PM
This is the working solution which worked for me fine
onChange catalog client script on Attachment type variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('variableName');
var ga = new GlideAjax('ValidateAttachmentFileName');
ga.addParam('sysparm_name', 'checkFileName');
ga.addParam('sysparam_attSysId', newValue);
ga.getXMLAnswer(function(answer) {
if (answer.toString() == 'invalid') {
g_form.showFieldMsg('variableName', 'Invalid file name', 'error');
}
});
}
Script Include: It should be client callable
var ValidateAttachmentFileName = Class.create();
ValidateAttachmentFileName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkFileName: function() {
var badChars = /[%$@()&<>'"]/;
var sysId = this.getParameter('sysparam_attSysId');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("sys_id", sysId);
gr.query();
if (gr.next()) {
if (badChars.test(gr.file_name.toString()))
return 'invalid';
else
return 'valid';
}
},
type: 'ValidateAttachmentFileName'
});
Output: Working fine
💡 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
12-03-2025 02:27 AM
@Ankur Bawiskar
When I am uploading the attachment in the attachment type variable, I am getting the below error.
