Tushar
Kilo Sage
Kilo Sage

I recently had a requirement where we needed to restrict attachments added to incident records in ServiceNow. The idea was to ensure that the file name of an attachment starts with a specific prefix based on the company's domain.

 

Goal

When a user adds an attachment to an incident, we want to check:

  • Does the incident have a company assigned?

  • If yes, get the company’s domain (from the u_alert_email field on the core_company table).

  • Use this domain to extract a prefix

  • Make sure the attachment file name starts with that prefix followed by an underscore (_), like:
    company.test_invoice_20250611.pdf

If it doesn’t match the pattern, we block the file upload with an error message

 

This is the Business Rule code (Before Insert on sys_attachment😞

 

(function executeRule(current, previous) {
    var loggerTAG = "[Validate attachment name] [" + current.sys_id + "]";

    try {
        if (!gs.isInteractive()) return; // Skip system-triggered inserts
        if (current.table_name !== 'incident') return; // Only run for incidents

        var incident = new GlideRecord('incident');
        if (!incident.get(current.table_sys_id)) {
            current.setAbortAction(true);
            gs.addErrorMessage('Invalid incident reference for this attachment.');
            return;
        }

        var companyId = incident.getValue('company');
        if (!companyId) {
            current.setAbortAction(true);
            gs.addErrorMessage('No company assigned to this incident.');
            return;
        }

        var company = new GlideRecord('core_company');
        if (!company.get(companyId)) {
            current.setAbortAction(true);
            gs.addErrorMessage('Could not retrieve company details.');
            return;
        }

        var domain = company.getValue('u_alert_email');
        if (!domain) {
            current.setAbortAction(true);
            gs.addErrorMessage('Domain not set for company.');
            return;
        }

        // get prefix
        var lastDot = domain.lastIndexOf('.');
        var prefix = lastDot > 0 ? domain.substring(0, lastDot).toLowerCase() : domain.toLowerCase();

        var fileName = current.getValue('file_name');
        if (!fileName || !fileName.toLowerCase().startsWith(prefix + '_')) {
            current.setAbortAction(true);
            gs.addErrorMessage(
                'Attachment name must start with "' + prefix + '_" (e.g., ' + prefix + '_aws_20250611).'
            );
        }

    } catch (e) {
        current.setAbortAction(true);
        gs.addErrorMessage('error occurred while validating attachment. Please contact admin.');
    }

})(current, previous);

 

This validation is lightweight, runs only in interactive mode, and helps the analysts, while working on multiple cases, don't mistakenly upload incorrect attachments—preventing potential data breaches due to misfiled files.

Let me know if you find this helpful or if you have any suggestions to improve it!!!

Comments
Tushar
Kilo Sage
Kilo Sage

It will look like this - 

 

image.png

 

Thanks,

Tushar

Version history
Last update:
‎07-01-2025 06:04 AM
Updated by:
Contributors