Enforcing Dynamic Mandatory Attachments Based on Account in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
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:
Dynamically checks if the logged-in user belongs to an excluded account.
Enforces attachments only for users not excluded.
Works across Service Portal and Mobile UI without relying on client-side UI logic.
Solution Overview
The solution uses:
Client Script (onSubmit) – to intercept the form submission and check attachments dynamically.
Script Include – server-side logic to determine if the current user’s account is excluded.
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
Dynamic and Configurable: Admins can update excluded accounts via system property.
Reusability: Works across multiple forms or portals without changes to the script.
Improved UX: Users immediately see error messages when attachments are required.
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.