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

@Gerrity 

You mean the file should be less than 100kb?

If yes we have a field called Size bytes in attachment field

So you can add one more addquery and check the file size 

Sample logic:

gr.addQuery("size_bytes","<","100000"); coverting kb into byte

and for accepting only png or jpg try this:

gr.addQuery("content_type","image/png").addOrCondition("content_type","image/jpeg");

Hope it helps

 

Thanks

Murthy

Thanks,
Murthy

@Murthy 

That's exactly what I'm looking for.  Thank you!

Quick question...I'd like to alert the user if the file is bigger than 100kb and not allow the upload.

Would this work (doesn't seem to alert the user when I test it)?

 

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.addQuery("size_bytes","<","100000")){
		return true;
	} else{
		alert('File size must be less than 100kb.');
		return false;
	}
	}
   }

 

thank you again!

Further to this..

 

Wondering if it's possible to alert the user before submitting the attachment to the attachment table?  It's too bad that the catalog item variable attachment's max_file_size is limited to integers and thus it's lowest value is 1mb.  

 

I'm looking for a way to check file size before submitting.

Wondering why this if statement is not working.  Everything is coming back as the "else"

 

function onSubmit() {
	var sys_id = g_form.getUniqueValue();
	var att = new GlideRecord('sys_attachment');
	att.addQuery('table_sys_id',sys_id);
	att.query();
	if (att.size_bytes > 100) {
		var confirm_box = confirm('Too Big!');
		return false;
    }
	else{
		var confirm_box2 = confirm('Size ok');
		return true;
	}
}

@Gerrity 

Are you attaching the attachement using attachement variable or using attachement clip icon under catalog?

If you are adding attachment using attachment variable we can try this poosiblity.

Let me know so that i will test in my PDI

 

Thanks,

Murthy

Thanks,
Murthy