Error message on Attachment field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hello, I have an attachment field on a catalog item that when the attachment has been deleted after being added to the field, an error message should display
This worked for a checkbox field...is it possible for an attachment field?
This is after the attachment was removed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
To ensure an attachment is mandatory in ServiceNow—even if a user adds and then removes a file—you can implement an onSubmit Catalog Client Script.
This script checks the attachment count at the time of submission and prevents submission if no files are attached, ensuring that at least one attachment is present.
Sample code :
function onSubmit() {
var sys_id = g_form.getUniqueValue(); // Get the current record's sys_id
var ga = new GlideAjax('CheckAttachment');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_table', g_form.getTableName()); // Gets the form's table name dynamically
ga.addParam('sysparm_sys_id', sys_id);
ga.getXMLAnswer(function(response) {
if (response === 'false') {
alert("CANNOT CONTINUE! All items in required information section MUST be met before continuing. Refer to Instructions for more information..");
return false;
}
});
return true; // Allow submission if attachment exists
}Create a Script Include (Server-Side)
var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasAttachment: function() {
var tableName = this.getParameter('sysparm_table');
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', tableName);
gr.addQuery('table_sys_id', sysId);
gr.query();
return gr.hasNext().toString(); // Returns 'true' or 'false'
}
});
Refer: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0723548
Also refer:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @DreDay3000 ,
In Service Catalog, attachments are a bit different from regular fields — they’re stored in the sys_attachment table, not directly in the catalog item record. That means you can’t just put an onChange client script on an “attachment field” the way you would for a text or choice field.
Client Script (onSubmit check)
- Instead of watching for attachment deletion in real time, you can validate at submission.
- Example: in an onSubmit client script, check if the record has attachments. If not, show an error and stop submission.
You can’t directly hook into attachment deleted” events in Catalog UI. The best practice is to validate at submission using `g_form.getAttachments()` and show an error if no attachments exist. This way, users can’t submit the request without the required file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @DreDay3000 ,
I tried your problem in my PDI and it works for me please check below steps
I created a onSubmit Client script and added below code
function onSubmit() {
//Type appropriate comment here, and begin script below
alert("Outside not attachment");
if (!g_form.getValue('add_attachment')) {
alert("Inside not attachment");
g_form.showFieldMsg('add_attachment', 'Please upload an attachment before submitting', 'error');
g_form.setMandatory("add_attachment", true);
return false;
}
return true;
}
Result
Initially when form gets load
When I click on Submit without adding attachment Also I got the error when I add and directly remove the attachment. Also making the attachment field as mandatory.
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
