Custom Attachment Validation for Specific File Types (.docx, .pdf) - Before Insert BR Not Working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2025 03:06 AM
I'm working on a custom table in ServiceNow and have a requirement to restrict the file types that can be attached to a specific custom file attachment field. I only want to allow .docx and .pdf files.
I attempted to use a Before Insert Business Rule(BR) on the sys_attachment table to validate the file extension, but it's not working as expected.
My Business Rule Details:
Table: sys_attachment
When: before
Insert: true
Condition: Table name is "custom_table_name"
Script:
(function executeRule(current, previous /*null when async*/) {
var validExtensions = ['pdf', 'docx'];
var fileName = current.file_name.toLowerCase();
var fileExtension = fileName.split('.').pop();
if (validExtensions.indexOf(fileExtension) === -1) {
gs.addErrorMessage('Invalid file type. For the Resume field, only the following formats are allowed: ' + validExtensions.join(', ').toUpperCase());
current.setAbortAction(true);
}})(current, previous);
When I attach an invalid file type (e.g., .png or .txt), the Business Rule does NOT seem to block the attachment, after choosing the file and clicking OK the browser does not respond.
Question:
1. Is a Business Rule on sys_attachment the correct way to handle attachment validation? I suspect the timing of the attachment process might be making the before BR unreliable or difficult to use for immediate client-side feedback.
2. What is the recommended, best-practice approach to restrict file types for attachments in custom table?
Any guidance or alternative solutions (Client Script, Script Include, etc.) would be greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2025 04:40 AM
recently I shared solution for something similar, you can check and enhance it
you need to write onChange client script on that field and use GlideAjax with client callable script include and check file names
Not Allow Special Characters in the attachment type variable in the Service Catalog
💡 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-09-2025 07:57 PM
Hope you are doing good.
Did my reply answer your question?
💡 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-12-2025 06:49 AM
I'm sorry I missed the last two days, I was unexpectedly sick.
i tried by making few changes in the code from the above solution, tried attaching .txt file but it did not work as expected.
below are the scripts which i used.
client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2025 06:59 AM
it worked for me.
what debugging did you do from your side?
💡 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
