The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Make Attachment Mandatory When User Selects "Yes" for the Variable in Catalog Item

Elango_C
Tera Contributor

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!

4 REPLIES 4

Elango_C
Tera Contributor

@Ankur Bawiskar @Dr Atul G- LNG @AJ-TechTrek Could you please help on this?

@Elango_C 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Elango_C_0-1747117123929.png

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. 

SwathiPedireddy
Kilo Sage

Hi @Elango_C 

 

Another option is to create variable for the attachment with type Attachment and make it mandatory using ui policy.

 

Thanks,

Swathi