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
3 hours ago
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
an hour ago
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
14m ago
This should work fine as we have something similar in use today. Since you are using a custom file attachment type field, notice that records are added to the sys_attachment table with the Table name field populated like 'ZZYYtable_name' so just change your Filter Condition to Table name contains custom_table_name.
