The CreatorCon Call for Content is officially open! Get started here.

onsubmit attachment validation is not working in order guide

lhieanne
Tera Contributor

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.

 

image.png 

 

Code below:

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

    // 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);
        }
    }
}
 
 
note that getxmlwait is not supported in portal. 
3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@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.

it's still submitting..

Ankur Bawiskar
Tera Patron
Tera Patron

@lhieanne 

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.

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