onsubmit attachment validation is not working in order guide
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 12:43 AM
Can someone help me with my onsubmit client script , it is not working properly in an order guide it is still submitting after the attachment validation is false.
Code below:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 12:50 AM
@lhieanne Can you try the following and see if it works for you.
function onSubmit() {
var appSysId = g_form.getValue('adt_application_name'); // 'adt_application_name' is the field ID
if (!appSysId) {
g_form.addErrorMessage('Application Sys ID is required.');
return false; // Prevent form submission if appSysId is empty
}
alert("Application Sys ID: " + appSysId);
var ga = new GlideAjax('global.AttachmentRequiredHelper');
ga.addParam('sysparm_name', 'getAttachmentRequired');
ga.addParam('appSysId', appSysId);
ga.getXMLAnswer(handleAttachmentRequiredResponse);
// Always prevent form submission initially
return false;
// This function handles the attachment-required response
function handleAttachmentRequiredResponse(response) {
try {
if (!response) {
alert('Error: No response from GlideAjax');
return;
}
var attachmentRequired = response.trim(); // Make sure there are no leading/trailing spaces
alert("Attachment Required Response: " + attachmentRequired);
if (attachmentRequired === 'true') {
alert('Attachment is required, checking for attachments...');
checkAttachments(g_form.getUniqueValue()); // Check for attachments if required
} else {
alert('Attachment is not required, submitting form...');
g_form.submit(); // Submit form if no attachment is required
}
} catch (e) {
alert('Error in handling the GlideAjax response: ' + e.message);
}
}
// This function checks for attachments
function checkAttachments(tableSysId) {
var gaAttachment = new GlideAjax('global.AttachmentRequiredHelper');
gaAttachment.addParam('sysparm_name', 'hasAttachments');
gaAttachment.addParam('tableSysId', tableSysId);
gaAttachment.getXMLAnswer(handleAttachmentsResponse);
return false; //Added this line to return when the second request was made
}
// This function handles the response to check for attachments
function handleAttachmentsResponse(response) {
try {
if (!response) {
alert('Error: No response from GlideAjax');
return;
}
var hasAttachments = response.trim(); // Remove extra whitespace
alert("Has Attachments Response: " + hasAttachments);
if (hasAttachments == 'false') {
alert('No attachments found, preventing form submission...');
g_form.addErrorMessage('Attachment is required because "Attachment Required" is set to True for the selected app.');
// Prevent form submission by returning false
return false;
} else {
alert('Attachments found, submitting form...');
g_form.submit(); // Submit the form if attachments are found
}
} catch (e) {
alert('Error in handling the GlideAjax response: ' + e.message);
}
}
}
I added a return false; at the last of function checkAttachments(tableSysId) {
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 12:57 AM
it's still submitting..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 01:12 AM
your script is using Ajax and it's asynchrounous Ajax call
By the time the script include function returns the form is submitted.
Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit
How To: Async GlideAjax in an onSubmit script
Asynchronous onSubmit Catalog/Client Scripts in ServiceNow
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