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.

Enforcing Dynamic Mandatory Attachments Based on Account in ServiceNow

Sushma Pavani K
Tera Guru

 

Use Case

In certain ServiceNow implementations, organizations require users to attach supporting documents when submitting a Record Producer or form.

However, the rules for mandatory attachments may vary depending on the account associated with the logged-in user. For example:

  • Users from specific accounts may be excluded from mandatory attachments.

  • All other users must attach at least one file before submission.

This article explains a dynamic solution that achieves this requirement using a Client Script with onSubmit() and a Script Include, avoiding hardcoding values and enabling easy maintenance via a system property.

 

Problem Statement

The standard “Mandatory Attachment” checkbox in ServiceNow has limitations:

  • It cannot differentiate based on user account.

  • It cannot dynamically enforce rules without modifying the client script.

  • Hardcoding account values makes future changes cumbersome.

As a result, organizations need a solution that:

  1. Dynamically checks if the logged-in user belongs to an excluded account.

  2. Enforces attachments only for users not excluded.

  3. Works across Service Portal and Mobile UI without relying on client-side UI logic.

 

Solution Overview

The solution uses:

  1. Client Script (onSubmit) – to intercept the form submission and check attachments dynamically.

  2. Script Include – server-side logic to determine if the current user’s account is excluded.

  3. System Property – to maintain a list of account sys_ids excluded from mandatory attachments.

 

Implementation Steps

1. Create a System Property

  • Name: sn_customerservice.glide.account.sysids.excluding.skip.attachment

  • Type: String

  • Value: Comma-separated sys_ids of accounts to exclude

This property allows admins to update excluded accounts without touching code.

 

2. Script Include

Create a Script Include named CheckAccountExclusion:

 

 
var CheckAccountExclusion = Class.create();
CheckAccountExclusion.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    validateAttachmentExclusionAccount: function() {
        var userID = gs.getUserID();
        var contactGR = new GlideRecord('customer_contact');
        contactGR.addQuery('sys_id', userID);
        contactGR.query();
        if (!contactGR.next()) return 'false';

        var userAccount = contactGR.getValue('account');
        if (!userAccount) return 'false';

        var excludedAccounts = gs.getProperty('sn_customerservice.glide.account.sysids.excluding.skip.attachment', '');
        if (!excludedAccounts) return 'false';

        var excludedList = excludedAccounts.split(',').map(function(s){ return s.trim(); });
        var isExcluded = excludedList.indexOf(userAccount) > -1;
        return isExcluded ? 'true' : 'false';
    },
    type: 'CheckAccountExclusion'
});

Explanation:

  • Retrieves the current user’s customer_contact record.

  • Checks the associated account.

  • Compares against a system property list to determine exclusion.

  • Returns 'true' or 'false' for client-side usage.

 

3. Client Script (onSubmit)

Create a Client Script on the Record Producer:

function onSubmit() {
    if (g_scratchpad.isFormValid) return true;

    var ga = new GlideAjax('CheckAccountExclusion');
    ga.addParam('sysparm_name', 'validateAttachmentExclusionAccount');
    ga.getXMLAnswer(setAnswer);

    return false;

    function setAnswer(answer) {
        if (g_form.getValue('u_case_type') == 'broken') {
            if (answer == 'false') { // user is NOT excluded
                if (document.getElementsByClassName('get-attachment').length == 0) {
                    g_form.addErrorMessage("Please attach a file before submitting.");
                    return false;
                }
            }
        }

        var actionName = g_form.getActionName();
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
}

Explanation:

  • Calls the Script Include via GlideAjax to check account exclusion.

  • If the account is not excluded and no attachment exists, submission is blocked.

  • Uses g_scratchpad to prevent multiple validations during the same submit.

 

Benefits

  1. Dynamic and Configurable: Admins can update excluded accounts via system property.

  2. Reusability: Works across multiple forms or portals without changes to the script.

  3. Improved UX: Users immediately see error messages when attachments are required.

  4. No Hardcoding: All logic is based on system property and current user’s account.

 

Conclusion

This solution provides a flexible, dynamic way to enforce mandatory attachments in ServiceNow.

By combining Client Scripts, Script Includes, and System Properties, administrators can easily maintain rules while ensuring compliance based on user accounts.

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Sushma Pavani K 

Thanks for sharing, but the approach has DOM manipulation which is not recommended.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader