How to strictly enforce mandatory Add Comments upon adding an attachment in Service Portal & SOW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi everyone,
I am relatively new to ServiceNow and am trying to implement a requirement on the Incident table.
The Requirement: When a user adds a new attachment to an existing Incident ticket, they must be forced to provide an explanation in the Additional Comments field. This needs to function seamlessly across both the Service Portal and Service Operations Workspace (SOW).
The Challenge: I understand that ServiceNow uploads and commits files to the sys_attachment table asynchronously the exact millisecond they are dropped onto the screen.
Because of this, I am running into two major roadblocks with standard onSubmit client scripts and before business rules:
The Form Bypass: Standard client scripts run asynchronously in Portal/Workspace, meaning the form often updates before the attachment count validation finishes.
The Window-Close Loophole: If a user uploads an attachment but simply closes the browser tab or abandons the page instead of clicking the Save/Update button, the attachment remains permanently saved on the ticket in the background without any comment.
What I need help with: What is the best architectural practice to handle this?
Is there a reliable way to intercept the native upload stream and delete/reject the file if a comment isn't present?
Alternatively, how can I structure an async-safe onSubmit script or a background scheduled job to clean up "orphaned" attachments if the user abandons the form by closing the window?
Any step-by-step guidance, Script Include, or Client Script patterns that are upgrade-safe for modern workspaces would be greatly appreciated!
Thanks in advance for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello @rajeshkole
One approach I can think of is to hide the standard attachment button on the Incident form and replace it with a custom "Add Attachment" UI Action (or Workspace Action).
When the user clicks the button, display a modal/pop-up that:
- Allows the user to upload the attachment.
- Requires the user to enter Additional Comments as a mandatory field.
- Only enables submission once both the attachment and comments have been provided.
After validation, the modal can:
- Upload the attachment.
- Update the Incident with the Additional Comments.
- Close the modal.
This approach avoids the challenges associated with the asynchronous attachment upload process, such as:
- Client-side validation being bypassed.
- Attachments being uploaded before form submission.
- Users closing the browser tab after uploading an attachment but before saving the record.
While it requires some customization, it provides much tighter control over the user experience and ensures that every attachment is accompanied by an explanation.
Hope this helps!
Thank You!
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @rajeshkole
You can combine a before update/insert Business Rule with an onSubmit Client Script for your requirement.
- Create a Before Business Rule
This rule prevents the form from saving if an attachment exists but the Additional Comments field is empty.
- Name: Enforce Comment on Attachment
- Table: Incident [incident]
- When: Before
- Insert / Update: True
- Condition: current.comments.changes()
Sample Script:
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'incident');
gr.addQuery('table_sys_id', current.sys_id);
gr.query();
if (gr.hasNext() && current.comments.nil()) {
gs.addErrorMessage(gs.getMessage('You must provide an explanation in Additional Comments when uploading an attachment.'));
current.setAbortAction(true);
}
})(current, previous);
- Create an onSubmit Client Script (For both Native UI & SOW)
- Name: Validate Comments on Attachment
- Table: Incident [incident]
- Type: onSubmit
Sample Script:
function onSubmit() {
var sysId = g_form.getUniqueValue();
var comments = g_form.getValue('comments');
var ga = new GlideAjax('AttachmentUtilAjax');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_sys_id', sysId);
ga.getXMLWait();
var hasAttachment = ga.getAnswer();
if (hasAttachment === 'true' && comments === '') {
g_form.setMandatory('comments', true);
g_form.showFieldMsg('comments', 'Please provide an explanation for the attached file(s).', 'error');
return false;
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hey @rajeshkole ,
I guess the before BR, is not much reliable in this case but, you can make use of the UI macro "attachment_entry" and maybe add your logic accordingly there, please take note as you need to be extra careful while handling this. As all the tables run the same logic of ui macro, so maybe try it first on PDI or something then on any prod or sub prod.
Now the browser tab close one, i think a safe solution can be a running a job maybe which can check for this incident just control the number of records it goes through and frequency accordingly. And a time frame where you can add the logic of maybe adding comment.
If my solution was helpful, then mark this as accepted. As this helps others finding the solution.
Regards
Kaustubh Dubey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
23 hours ago
wouldn't this requirement be an overhead for the agents to add comments all the time when file is added?
I believe this is not a valid business requirement and should not be done
not every requirement is meant to be developed
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader