Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Conditional Mandatory Attachment

leahdany
Giga Guru

I'm trying to require an attachment based on a condition. I have done this in the past for other projects but it's simply not working this time. I have tried a few answers I found from the Community but absolutely nothing is working. I am in the Human Resources: Service Portal application. I have a record producer that I've put an onSubmit client script on. I put alerts in to validate that my if statement is evaluating to true and it is, and my alert in the try statement is also firing. Here's my client script:

function onSubmit() {

    // If employee's country is Ireland or United Kingdom and Reason for Change is Salary & Payments require an attachment
    var reason = g_form.getValue('u_tcReason');
    var country = g_form.getValue('user_country');

    if (reason == 'Salary & Payments' && country == 'GB' || country == 'IE') {
        alert('in the if statement for my condition. Country is ' + country);
        try {
			alert('in the try');
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
                alert('Need attachments.');
                return false;
            }
        } catch (e) {
            var count = getSCAttachmentCount();
            if (count <= 0) {
                alert('Need attachments.');
                return false;
            }
        }
    }
}

Isolate script: false

UI Type: All

I've also tried this approach:

function onSubmit(){
var value = g_form.getValue('request_type');

if(value == 'b' || value == 'd'){

if(this.document.getElementsByClassName('get-attachment').length == 0) {
		alert('You must add an attachment before submitting this request.');
		return false;
	}

}
}

That doesn't work either. With both approaches I get this error message at the top of the form in native:

find_real_file.png

13 REPLIES 13

Hi,

Few things to check

1) is this working fine with admins

2) Is this issue coming with non-admins

3) when was the last time it worked fine

refer below links

Generic error on form: "Submit canceled due to a script error - please contact your System Administr...

Regards
Ankur

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

@Ankur Bawiskar 

  1.  No, it gives this error with admins find_real_file.png
  2. The error with non-admins is find_real_file.png
  3. This is a new client script so therefore it has never worked on native.

That link says to resolve this I'd have to issue the client_script_admin role. No one currently has that role, not even admins.

Hi,

1st error means document object is not working

But you already have this configured

Isolate script: false

UI Type: All

If the issue couldn't be debugged then raise HI ticket

Regards
Ankur

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

I'll open a HI ticket and report back with the findings. Thanks!

Instead of doing this, you should write GlideAjax from client script and query sys_attachment table in script include function.

Script include will return result of sys_attachment query and you can use that in your client script.

This will work for sure in scoped app since same solution works for me.

 

Regards,

Sachin