Preventing form submission when there is no attachment

lhieanne
Tera Contributor
 
14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

@lhieanne 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

this is for catalog item in portal, then attachment should be mandatory when the selected application has attachment required = true in the table.

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

tried the code but still keeps on submitting