Make Attachment Mandatory When User Selects "Yes" for the Variable in Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2025 12:30 AM
Hi all,
I'm working on a Service Catalog item in the Employee Service Center (ESC), and I have a requirement that I'm struggling to implement.
I have a Yes/No variable on the catalog item that asks:
"Have you attached the proof for training?"
Requirement:
If the user selects "Yes", they must add an attachment before submitting the form.
If they select "No", the form can be submitted without an attachment.
If they select "Yes" but do not attach a file, the form should not submit, and an error message should be shown.
I'm using a Catalog Client Script (onSubmit) to try and enforce this behavior. Here's what I've tried so far:
function onSubmit() {
if (g_form.getValue("attached_proof_of_training") === "yes")
{
if (this.document.getElementsByClassName('get-attachment').length == 0)
{
g_form.addErrorMessage("Attachment is mandatory when Yes is selected.");
return false;
}
}
}
However, this doesn't seem to be working — the form still submits even when "Yes" is selected and no file is attached. I'm working within ESC (not Service Portal).
While I try to save this script, I'm getting an Info Message stating, "This Catalog Client Script is not VA Supported due to the presence of the following variables in the script: this.document.getElementsByClassName.length"
Can someone explain the meaning and possible cause of this info message, and how to resolve it?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2025 03:17 AM
@Ankur Bawiskar @Dr Atul G- LNG @AJ-TechTrek Could you please help on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2025 06:25 AM
try this and it will work in both native and portal for catalog item
function onSubmit() {
//Type appropriate comment here, and begin script below
try {
var count = getRPAttachmentCount();
if (window == null) {
// portal
if (this.document.getElementsByClassName('get-attachment').length == 0 && g_form.getValue("attached_proof_of_training") === "yes") {
g_form.addErrorMessage("Attachment is mandatory when Yes is selected.");
return false;
}
}
} catch (ex) {
// native view
var length = getSCAttachmentCount();
if (length == 0 && g_form.getValue("attached_proof_of_training") === "yes") {
g_form.addErrorMessage("Attachment is mandatory when Yes is selected.");
return false;
}
}
}
function getRPAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item_producer").scope().attachments.length;
} catch (e) {
length = -1;
}
return length;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2025 11:24 PM
While I try to run the script that you have provided, I'm getting the above info message and also when I select 'Yes' for the field and try to submit the form without attaching any files, the form still submits. The form should prevent submission in this case.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2025 08:06 AM
Hi @Elango_C
Another option is to create variable for the attachment with type Attachment and make it mandatory using ui policy.
Thanks,
Swathi