onsubmit catalog client scripts

TMKAM
Tera Contributor

I have onsubmit catalog script and i want to set a mandatory field for attachment when user select both options as per below.

 

When I select Request Type (variable A) is New Cert (variable B) and Use Case Type (variable C) is Public (variable D) the attachment is mandatory field.

 

this is the onsubmit script currently i have.

 

"function onSubmit() {

var method = g_form.getValue('variable A');
if (method == 'variable B'){
    if (this.document.getElementsByClassName('get-attachment').length == 0) {
        //var getName = g_user.getFullName();
        alert("You must add attachments before submitting this request.");
        return false;
    }
}
}"
 

 

TMKAM_1-1727237426580.png

 

thank you

 

7 REPLIES 7

Eshwar Reddy
Kilo Sage

Please see the screenshot below for the code required to make attachments mandatory.
Cline script 

EshwarReddy_0-1727238588293.png

 



Script include

EshwarReddy_1-1727238588064.png

 

 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

Thanks 
Eshwar

Hi Eshwar,

the screenshot you have attach is too small and blurry... can you copy the scripts in here.

thank you 

Hi @TMKAM 

 

Update script :-

function onSubmit() {
// Get values of both variables
var requestType = g_form.getValue('variable_A'); // Replace with the actual variable name for Request Type
var useCaseType = g_form.getValue('variable_C'); // Replace with the actual variable name for Use Case Type

// Check if Request Type is "New Cert" and Use Case Type is "Public"
if (requestType == 'variable_B' && useCaseType == 'variable_D') {
// Check if any attachments exist
if (this.document.getElementsByClassName('attachment_list_rows').length == 0) {
alert("You must add attachments before submitting this request.");
return false; // Prevent form submission
}
}

// Allow form submission if conditions are not met
return true;
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi ravi gaurav,

i have change a bit the catalog client scripts because the scripts still prompt the alert even after i have attach the file.

 

thank you

 

Update script :-

function onSubmit() {
// Get values of both variables
var requestType = g_form.getValue('variable_A'); // Replace with the actual variable name for Request Type
var useCaseType = g_form.getValue('variable_C'); // Replace with the actual variable name for Use Case Type

// Check if Request Type is "New Cert" and Use Case Type is "Public"
if (requestType == 'variable_B' && useCaseType == 'variable_D') {
// Check if any attachments exist
 if (this.document.getElementsByClassName('get-attachment').length == 0) {
        //var getName = g_user.getFullName();
        alert("You must add attachments before submitting this request.");
        return false;
}
}

}