How to make attachment mandatory

rajeshraji1
Tera Expert

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:

  1. The Test is Yes condition.
  2. The attachment upload process.
  3. The logic that shows/hides the popup.
  4. The form submission handling.
    client script : 
    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(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 check
                    g_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 :
  5. 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 
4 REPLIES 4

Brian Lancaster
Tera Sage

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;
    }
}

HI, This code is not work as logic

SasiChanthati
Giga Guru

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

  1. 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)
  2. 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:

  1. Using a window-level flag (attachmentAlreadyChecked) to track if we've already verified attachments
  2. Using gsftSubmit() instead of g_form.submit() to properly resubmit the form
  3. 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.

hi @SasiChanthati  Still facing same issue not work