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.

Restrict file extensions for File Attachment field type

Eduard T
Tera Contributor

Hello,

 

I have been looking for an attribute that could restrict the file extensions for the File Attachment field type on a table level, not on a global level (in order to only allow PDFs to be attached) but couldn't find any. Are you aware of any solution for this? Thank you!

4 REPLIES 4

Musab Rasheed
Tera Sage

Hello,

allowed_extensionsSpecifies a list of allowed file types. For example, allowed_extensions=txt;pdf.

Applicable variables: Attachment.

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/product/service-catalog-management/...

https://www.servicenow.com/community/it-service-management-forum/i-have-created-a-catalog-item-in-wh...

https://www.servicenow.com/community/it-service-management-forum/need-to-restrict-the-attachment-var...

 

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hi @Musab Rasheed ,

I am talking about the File Attachment field type, not about the Attachment catalog variable. The allowed_extensions attribute doesn't seem to work on the File Attachment field

Hello,

Have you find the solution for your issue ? 

To restrict file extensions for attachments, you'll need to approach the problem from a server-side perspective using a Business Rule or a Script Include that runs when an attachment is added to the sys_attachment table.

Here’s how you can do it using a Business Rule on the sys_attachment table:

Steps to Restrict File Extensions for Attachments

  1. Create a Business Rule:

    • Navigate to System Definition > Business Rules.
    • Click on New to create a new Business Rule.
  2. Configure the Business Rule:

    • Name: Give it a meaningful name (e.g., Restrict File Extensions).
    • Table: Select sys_attachment as the table.
    • When: Select before for when you want the rule to run.
    • Insert: Check the Insert checkbox, as we want the rule to run when a new attachment is inserted.
  3. Add the Script to the Business Rule:

    (function executeRule(current, previous /*null when async*/) {
    var validExtensions = ['pdf', 'jpg', 'png']; // List of allowed extensions
    var fileName = current.file_name.toLowerCase();
    var fileExtension = fileName.split('.').pop(); // Get the file extension

    // Check if the file extension is not in the list of allowed extensions
    if (validExtensions.indexOf(fileExtension) === -1) {
    gs.addErrorMessage('Invalid file type. Only ' + validExtensions.join(', ') + ' are allowed.');

    // Abort the attachment insertion
    current.setAbortAction(true);
    }
    })(current, previous);