How to make attachment mandatory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-29-2025 10:35 AM
HI, when a specific State Test is set to Yes. The goal is to allow form submission only after the attachment has been successfully uploaded; otherwise, a popup should appear. You've implemented code that successfully triggers the popup when the attachment is missing, but even after uploading the attachment, you're still unable to submit the form.
To help me identify the issue, please provide the relevant code snippets, especially the parts dealing with:
- The Test is Yes condition.
- The attachment upload process.
- The logic that shows/hides the popup.
- The form submission handling.
client script :function onSubmit() {var testVal = g_form.getValue('test'); // Replace with your actual variable nameif (testVal === 'Yes') {var ga = new GlideAjax('CheckAttachmentCount');ga.addParam('sysparm_name', 'hasAttachment');ga.addParam('sys_id', g_form.getUniqueValue());ga.getXMLAnswer(function(answer) {if (answer !== "true") {g_form.addErrorMessage("Attachment is required when Test is set to Yes.");// Prevent submission by not calling anything} else {// Force submit after passing async checkg_form.setSubmit(true);g_form.submit();}});return false; // Prevent immediate submission until async check finishes}return true; // Submit normally if Test is not 'Yes'}
Script include : - var CheckAttachmentCount = Class.create();CheckAttachmentCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {hasAttachment: function() {var sys_id = this.getParameter('sys_id');var gr = new GlideSysAttachment();var attachments = gr.getAttachments('sc_cart_item', sys_id);return attachments.hasNext(); // true if at least one attachment exists}});Please suggest on this

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-30-2025 12:56 PM
Try calling the the getXML as a function like this instead.
function onSubmit() {
var testVal = g_form.getValue('test'); // Replace with your actual variable name
if (testVal === 'Yes') {
var ga = new GlideAjax('CheckAttachmentCount');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(response);
}
}
function response(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer !== "true") {
g_form.addErrorMessage("Attachment is required when Test is set to Yes.");
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-31-2025 06:14 AM
HI, This code is not work as logic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-30-2025 01:47 PM
Looking at your code, I can see why you're experiencing an issue with form submission even after uploading an attachment. The problem is in how you're handling the form submission flow in your client script. Here's the analysis and solution:
The Issue
Your current implementation has a logical flow problem:
- You're calling g_form.setSubmit(true) and g_form.submit() inside the AJAX callback
- However, the onSubmit() function has already returned false to prevent submission
- Once onSubmit() returns false, the form submission is already canceled, and calling g_form.submit() later starts a new submission cycle (which will trigger onSubmit() again)
This creates a loop where:
- User submits form
- onSubmit() returns false to wait for attachment check
- Attachment check passes
- Code calls g_form.submit()
- This triggers onSubmit() again, repeating the cycle
Solution
Here's how to fix it:
// Client Script function onSubmit() { var testVal = g_form.getValue('test'); if (testVal === 'Yes') { // Store a flag to avoid infinite loops if (window.attachmentAlreadyChecked) { // Reset the flag for future submissions window.attachmentAlreadyChecked = false; return true; } var ga = new GlideAjax('CheckAttachmentCount'); ga.addParam('sysparm_name', 'hasAttachment'); ga.addParam('sys_id', g_form.getUniqueValue()); ga.getXMLAnswer(function(answer) { if (answer !== "true") { g_form.addErrorMessage("Attachment is required when Test is set to Yes."); // Don't do anything else - let the user add an attachment } else { // Set flag to avoid loop window.attachmentAlreadyChecked = true; // Submit the form again gsftSubmit(null, g_form.getFormElement(), 'submit'); } }); return false; // Prevent immediate submission } return true; // Submit normally if Test is not 'Yes' }
The key changes are:
- Using a window-level flag (attachmentAlreadyChecked) to track if we've already verified attachments
- Using gsftSubmit() instead of g_form.submit() to properly resubmit the form
- Properly resetting the flag to avoid issues with future submissions
Your Script Include looks correct and doesn't need changes. It properly checks for attachments on the specified record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-31-2025 06:14 AM
hi @SasiChanthati Still facing same issue not work