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.

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

Saurabh Gupta
Kilo Patron

Hi,
Please refer below script

 

	
	try {
		
		length = angular.element("#sc_cat_item").scope().attachments.length;
		
	} catch(e) {
		
		length = -1;
		
	}
	if(length>0)
alert(""attachment is there);


	return length;

 

You can create a function in UI script as below 

SaurabhGupta_0-1673454361988.png

 

In the portal theme widget's JS include related list create a record as below

 

SaurabhGupta_1-1673454458744.png

 

SaurabhGupta_2-1673454492135.png

 

Now in onsubmit client script use below script

var count = getSCAttachmentCount();
alert("Attcah Count: "+count )

 

 

 

 


Thanks and Regards,

Saurabh Gupta

Omkar Kumbhar
Mega Sage

Hello @Anubhav Srivas1 ,

You can try below onsubmit client script

function onSubmit() {

 

var cat_id = gel('sysparm_item_guid').value;

 

var gr = new GlideRecord("sys_attachment");

 

gr.addQuery("table_name", "sc_cart_item");

 

gr.addQuery("table_sys_id", cat_id);

 

gr.query();

 

if (!gr.next()) {

 

alert("You must attach a file to submit.");

 

return false;

 

}

 

}

 

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

Hello Omkar,

 

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

}
}

}

Alp Utku
Mega Sage

Please try the below catalog client script (onSubmit)

 

 

 

function onSubmit() {
 
    if (g_form.getValue('your_variable') == "your_variable_value") { // 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;
            }

        }
    }

}