Issue with making attachment variable mandatory before submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 10:12 PM
Hello,
I'm trying to prevent a form from submitting if certain checkboxes are checked and set the "add_attachments" field mandatory if attachments are not included before submitting. The onSubmit catalog script I have:
function onSubmit() {
//Type appropriate comment here, and begin script below
var a = g_form.getValue('a');
var b = g_form.getValue('b');
var c = g_form.getValue('c');
var d = g_form.getValue('d');
var e = g_form.getValue('e');
var f = g_form.getValue('f');
var g = g_form.getValue('g');
var h = g_form.getValue('h');
var i = g_form.getValue('i');
if ( (a == "true" || b == "true" || c == "true" || d == "true" ||
e == "true" || f == "true" || g == "true" || h == "true" || i == "true") ) {
g_form.addErrorMessage('Please add the required attachments.');
g_form.setMandatory('add_attachments', true);
}
}
The idea I currently have is to not initially set the add_attachment field to mandatory because one field does not require an attachment. If the other checkboxes, a - i, are checked, and the user forgets to add an attachment before submitting, the error message appears, the add_attachment field becomes mandatory, and the submit becomes false (does not submit). But this code is still submitting. Any advice?
What I trying to do is
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 10:38 PM - edited 12-18-2024 10:41 PM
Hi @TerryC03 ,
You can do like below.
function onSubmit() {
// Get the sys_id of the current record
var recordSysId = g_form.getUniqueValue();
// Create a GlideRecord object to check for attachments
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', recordSysId);
attachmentGR.query();
// If there are no attachments, prevent submission and show an error message
if (!attachmentGR.next()) {
// Show a message to the user
g_form.showFieldMsg('attachments', 'Please add at least one attachment before submitting.', 'error');
// Return false to prevent the form from being submitted
return false;
}
// Allow form submission if attachments exist
return true;
}
check this blog for preventing form summation using On submit client script: https://servicenowwithrunjay.com/restrict-user-to-submit-form-via-onsubmit-client-script/
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 10:56 PM