&& operator is not working

TerryC03
Tera Expert

Hello,

 

May someone assist me in troubleshooting my code? I trying to prevent a user from submitting a form if a specific checkbox is not true (checked) as well as if there are no attachments on the form. However, the AND operator is not working as expected.

 

 

 

var checkbox = g_form.getValue('test');  
var countRequired = 0;
        if (window == null) {

            // portal
            if ( (this.document.getElementsByClassName('get-attachment').length == countRequired) &&
				(checkbox == true) ) {
                g_form.addErrorMessage('Please add the required attachments.');
                return false;
            } else {
                // native view
                var length = $j("li.attachment_list_items").find("span").length;
                if ( (length == countRequired) && (checkbox == true) ) {
                    g_form.addErrorMessage('Please add the required attachments.');
                    return false;
                }
            }
        }

 

 

With this code, the submission goes through with a javascript error.

 

Thanks

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

'checkbox' is not defined - replace this with g_form.getValue('var_name'), and change the value to 'false' as you are now attempting to prevent submission if the box is checked.  Also, DOM manipulation does not work in Portal, so you'll need a different approach for this like a variable with the type Attachment.  You can use this in the native UI also, or a GlideAjax that checks the sys_attachment table for a record.

Thank you for your response. The original code has checkbox defined ( I just forgot to add it in this code). With the use of the OR operator, the code does work. I would like to avoid an attachment variable due to possible confusion on the portal.

I get that - I'm not a big fan of that variable myself, but it would be useful in this case. So you want to prevent submit if a checkbox variable is checked AND there are no attachments?  I've had better luck with checkbox values using 'true' and 'false' instead of true and false.  Make sure the isolate script box is unchecked, then the native UI should work.

Here are some other ways to consider

https://www.servicenow.com/community/now-platform-blog/making-attachments-mandatory-in-sp-the-ultima... 

https://www.servicenow.com/community/developer-articles/possible-ways-for-making-an-attachment-manda...