onsubmit client script to check for attachments before submitting

Anubhav Srivas1
Tera Expert

Hello Experts,

 

I need a onsubmit client sprint which should check for attachments if the request_type = grant or modfiy.

 

system should not allow the request to be submitted until the attachment is attached.

 

for revoke it should not check for attachment and allow order now.

7 REPLIES 7

Hello,

 

I'm getting a javacript error on the console. please help.

 

this is my script

function onSubmit() {

if (g_form.getValue('request_type') == 'grant'); { // This is the field value of the question you want to check
try {

var msg = 'This request requires an attachment. Please review the form and try again.';

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert(msg);
return false;
}

}

//For Service Portal
catch (e) {
var count = getSCAttachmentCount();
if (count <= 0) {
alert(msg);

return false;
}

}
}

}

Heelo Alp,

 

i'm using this script but getting an error

 

function onSubmit() {

if (g_form.getValue('request_type') == 'grant'); { // This is the field value of the question you want to check
try {

var msg = 'This request requires an attachment. Please review the form and try again.';

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert(msg);
return false;
}

}

//For Service Portal
catch (e) {
var count = getSCAttachmentCount();
if (count <= 0) {
alert(msg);

return false;
}

}
}

}

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Anubhav Srivas1 ,

 

Create the catalog client script as below. it will validate for at least one attachment is mandatory before submission of request. Please cross check grant and modify backend values.

function onSubmit() {

    if ((this.document.getElementsByClassName('get-attachment').length < 1) && (g_form.getValue('request_type') == 'grant' || g_form.getValue('request_type') == 'modify')) {
        alert('Please ensure that atleast 1 attachment required to submit the catalog');
        return false;

    }

}

 

GunjanKiratkar_0-1673501090222.png

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy