onsubmit catalog client scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 09:14 PM
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() {
thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 11:26 PM
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/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2024 12:02 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 11:41 PM
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