Error message on Attachment field

DreDay3000
Tera Guru

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

Screenshot 2026-04-01 125630.png

 This worked for a checkbox field...is it possible for an attachment field?

 

Screenshot 2026-04-01 125740.png

 This is after the attachment was removed

 

Screenshot 2026-04-01 125844.png

3 REPLIES 3

Tanushree Maiti
Kilo Patron

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:

https://www.servicenow.com/community/itsm-forum/display-error-message-for-attachment-mandatory/m-p/2....

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

pavani_paluri
Tera Guru

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 it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Sarthak Kashyap
Mega Sage

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;


}
 

SarthakKashyap_1-1775069421557.png

 

Result 

Initially when form gets load 

SarthakKashyap_2-1775069461128.png

 

 

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.

SarthakKashyap_3-1775069506342.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak