Restrict on type of attachment attached

shweta32
Kilo Contributor

Hello,

I have a requirement in which I need to allow only attachments with extension .xls or .xlsx and this needs to be done only for a particular catalog item.

Kindly suggest. 

12 REPLIES 12

Aviansh
Tera Contributor

Hi Shwetha,

 

I tried and replicated the same as you did, but its not working.

In js includes, its asking to create UI Script , so i have used your script in that and called that scrpt in onSubmit CS.

Could you please suggest me on this.

 

Many thanks in advance.

 

Akshay37
Mega Guru

Hi Shweta, Adittyaa,

 

Create one system property for types you want to allow in sys_properties table in that property put comma separated values of content type you want to allow. Content type you can get from sys_attachment table check for any existing attachment  for .xls or .xlsx check the content type and put it in property you have created.

after that Create before insert business rule on sys_attachment table and put below code.

Note: Apply your table condition or any specific condition so that it will not run for other tables

 

var arr = gs.getProperty('property_name').split(','); //specify property name 

if(!arr.indexOf(current.content_type.toString()) >= 0 )
{
gs.addErrorMessage('Selected file type is restricted');
current.setAbortAction('true');
}

 

Mark this correct/helpful if this helps you.

 

Thanks,

Akshay

SOUMYA09
Mega Guru

Hi Shweta,

An easy method to achieve the same is;

While creating a catalog item of type: 'Attachment', of the 'Type Specification' section in the field 'Variable Attribute', we can simply mention a line of code: allowed_extensions=xls;xlsx. This is what I found in ServiceNow docs: Service Catalog variable attributes

 

This will without any exclusive scripting do the work for us. It even presents an error message if another type of file is added other than the specified.

 

Please feel free to mark the answer helpful and correct.

 

Kind regards,

Soumya