Preventing form submission when there is no attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2025 07:51 PM - edited ‎01-31-2025 12:46 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2025 08:14 PM
this is for catalog item?
is this for portal only?
Should the attachment be mandatory based on some variable?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2025 08:40 PM - edited ‎01-07-2025 08:40 PM
this is for catalog item in portal, then attachment should be mandatory when the selected application has attachment required = true in the table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2025 08:18 PM
Hi @lhieanne ,
try below code.
function onSubmit() {
var appSysId = g_form.getValue('adt_application_name');
// Check if the application Sys ID is provided
if (!appSysId) {
g_form.addErrorMessage('Application Sys ID is required.');
return false; // Prevent form submission
}
// Use GlideAjax to determine if attachments are required
var ga = new GlideAjax('global.AttachmentRequiredHelper');
ga.addParam('sysparm_name', 'getAttachmentRequired');
ga.addParam('appSysId', appSysId);
ga.getXMLAnswer(function(response) {
if (!response) {
alert('Error: No response from GlideAjax for attachment requirement check.');
return false; // Prevent form submission
}
var attachmentRequired = response;
if (attachmentRequired) {
checkAttachments(g_form.getUniqueValue());
} else {
g_form.submit(); // Submit the form if attachments are not required
}
});
// Always prevent form submission initially
return false;
// Function to check for attachments
function checkAttachments(tableSysId) {
var attachmentGa = new GlideAjax('global.AttachmentRequiredHelper');
attachmentGa.addParam('sysparm_name', 'hasAttachments');
attachmentGa.addParam('tableSysId', tableSysId);
attachmentGa.getXMLAnswer(function(response) {
if (!response) {
alert('Error: No response from GlideAjax for attachment check.');
return false; // Prevent form submission
}
var hasAttachments = response;
if (hasAttachments) {
g_form.submit(); // Submit the form if attachments are found
} else {
g_form.addErrorMessage('Attachment is required because "Attachment Required" is set to True for the selected application.');
}
});
}
}
-------------------------------------------------------------------------
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
‎01-07-2025 08:39 PM
tried the code but still keeps on submitting