Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make " Add attachments" mandatory and prevent from submission based on Variable selection

Naresh_5120
Tera Contributor

Dear Community,

 

I need some suggestion to make " Add attachments" mandatory in catalog item and prevent from submission the catalog item form  based on Yes/NO Type Variable selection. If Variable called "do_have_approval" is Yes then make the " Add attachments" mandatory in catalog item and prevent from submission the catalog item form, if attachment is attached then allow to submit the form. 

Naresh_5120_0-1764659524713.png

 



I am trying to achieve this through catalog client script but its not working.

1 REPLY 1

Sarthak Kashyap
Mega Sage

Hi @Naresh_5120 ,

 

I tried your problem in my PDI and it works for me please check solution below 

 

I created 2 variables on my Catalog item 1. Is Approval(Yes/No), 2. Attachment (attachment type)

SarthakKashyap_0-1764661617249.png

 

 

I created one OnSubmit Client script and add below code 

SarthakKashyap_1-1764661645940.png

 

function onSubmit() {
   //Type appropriate comment here, and begin script below

   var approval = g_form.getValue('is_approval');  // Yes/No variable

    if (approval === 'Yes') {

        var attCount = g_form.getControl('add_here').attachments
            ? g_form.getControl('add_here').attachments.length
            : 0;

        if (attCount === 0) {
            g_form.addErrorMessage("Attachment is required when approval is Yes.");
            return false;  
        }
    }

    return true;
   
}

 

Result

When I select yes and try to submit it shows me error

SarthakKashyap_2-1764661693151.png

 

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

Thanks and Regard,

Sarthak