Preventing form submission when there is no attachment

lhieanne
Tera Contributor
 
14 REPLIES 14

Muhammad 1
Tera Contributor

@lhieanne 

Could you ensure that you pass the sys_id in the script include, and use == (double equals) instead of === (triple equals)? This is because it === checks both value and type and if the response type differs, it will throw an error. Additionally, kindly share the script include code.

Muhammad 1
Tera Contributor

@lhieanne 

Ensure that you are passing the sys_id script include, and use == (double equals) instead of === (triple equals). This is because it === checks both value and type and if the response type differs, it will throw an error. Additionally, kindly share the script including the code.

here is the include

var AttachmentRequiredHelper = Class.create();
AttachmentRequiredHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getAttachmentRequired: function() {
       
        var appDisplayName = this.getParameter('appSysId'); // Get the Display Name passed from the client script
       
        // Log the Display Name for debugging purposes
        gs.info('Display Name received by Script Include: ' + appDisplayName);  // This will log the Display Name
        // Query the u_adt_application_support table for the provided Display Name
        var gr = new GlideRecord('u_adt_application_support');
        gr.addEncodedQuery('u_adt_active=true^u_application=' + appDisplayName);
        gr.query();
        // Check if a record is found and return the attachmentRequired value
        if (gr.next()) {
            var attachmentRequired = gr.u_attachment_required.toString(); // Convert Boolean to string
            // Log the attachmentRequired value for debugging
            gs.info('Attachment Required for application ' + appDisplayName + ': ' + attachmentRequired);
            return attachmentRequired;
        }
        // Log when no records are found
        gs.info('No record found for Display Name: ' + appDisplayName);
       
        // Return 'false' if no records are found
        return 'false';
    },

    hasAttachments: function() {
        var tableSysId = this.getParameter('tableSysId');

        var attachment = new GlideRecord('sys_attachment');
        attachment.addQuery('table_sys_id', tableSysId);
        attachment.query();

        return attachment.hasNext().toString();  // Return 'true' if attachments are found, otherwise 'false'
    },

    type: 'AttachmentRequiredHelper'
});

@lhieanne 

 

try this code : 

function onSubmit() {
    var appSysId = g_form.getValue('adt_application_name'); 

 

 

    if (!appSysId) {
        g_form.addErrorMessage('Application Sys ID is required.');
        return false;  // Prevent form submission if apprised 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;
            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 attachmentGa = new GlideAjax('global.AttachmentRequiredHelper');
        attachmentGa.addParam('sysparm_name''hasAttachments');
        attachmentGa.addParam('tableSysId', tableSysId);
        attachmentGa.getXMLAnswer(handleAttachmentsResponse);
    }

 

    // 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;
            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.');
                return false;  // Prevent form submission if there are no attachments
            } 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);
        }
    }
}

jaycoover
Tera Expert

Does this even work?

function onSubmit() {
    return false:
}

If not, maybe start debugging on the client in Dev tools.