Script to Run on catalog item to Count and Limit Attachment Number

satya30
Tera Contributor

HI @Ankur Bawiskar 

I need to limit the number of attachments to 1  on the catalog item while ordering 

i have checked yours previous answer available here Limit number of attachements on catalog Item - Developer Community - Question - ServiceNow Community but it not working.

i written some code to find attachment is attached or not, but how to add filter to count the number of attachments

20 REPLIES 20

can u please let me know how can i do that?

since there is a requirement to do

 

Hi,

this should work in native; will have to check for portal

Ensure Isolate Script Field is set To False for this client script for DOM to work

function onSubmit() {
	//Type appropriate comment here, and begin script below
	var arr = [];

	if(window != null){
		$j("a.content_editable").each(function( index ) {
			var val = $j(this).text();
			arr.push(val.toString());
		});
		alert('File Names are'+ arr);
		return false;
	}

}

Output: it gave me 2 files names then you can iterate over each and check the file name starts with keyword or not

once you get all the file names you can iterate and check if it is allowed or not

Please mark my response as correct and close the thread

Regards
Ankur

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

Murthy Ch
Giga Sage

@satya 

 

Try this if you want to allow only excel files

function onSubmit() {

var sys_id =   g_form.getUniqueValue();
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',sys_id);
gr.query();
if(gr.next()){
if(gr.getValue('file_name').indexOf('.xlsx') != -1 || gr.getValue('file_name').indexOf('.pdf') != -1){
return true;
}
else{
return false;

}
}


Thanks,

Murthy

Thanks,
Murthy

but i also need to limit the number of attachments

Gerrity
Tera Expert

Could this be modified to restrict the file attachment size to less than 100kb and only to png or jpg?

I'd like this on a client side script so that before user clicks submit it would check the file attachment for both size and extension.

 

The attachment variable of max_file_size seems to be restricted to mbs so the lowest is 1mb.

 

Thank you