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

Can you try thr below :-

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') {
var countRequired = 1; // Number of required attachments

if (window == null) {
// Portal view
if (this.document.getElementsByClassName('get-attachment').length < countRequired) {
alert('You must add 1 attachment before submitting this request.');
return false; // Prevent form submission
}
} else {
// Native view
var length = $j("li.attachment_list_items").find("span").length;
if (length < countRequired) {
alert('You must add 1 attachment 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/

Community Alums
Not applicable

Client Script - 

function onSubmit() {
    var award = g_form.getValue('type_of_award_appreciation');
    var cate = g_form.getValue('category');

    if (award == 'Pat on the Back Award' && cate == 'hardware') {
        g_form.setMandatory('attachment', true);
        return false;
    }
}

 

aryanjain25
Giga Guru

Hi @TMKAM ,

I posting a small code example below which you can tweek as per your requirement. 

 

function onSubmit(){
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


if (requestType == 'variable_B' && useCaseType == 'variable_D') {

g_form.setMandatory('field_name', true);
return false; // Prevent form submission
}
}

 

I would appreciate if you can mark this response as correct or helpful if it helped you with your question.

 

Thanks,

Aryan Jain