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.

How to restrict user to add specific file extension on catalog item

now_dev
Mega Expert

Hi All,

I have created a custom attachment macro and I wanted to restrict the user to add only PDF, Doc, DocX,txt,xlsx and xls file extension.

find_real_file.png

I tried using below script which I got from To get Attachment file name and file format   but it's not working. Also, I can't use properties as it will implement the chnages globally. Please suggest.

function onSubmit() {

//var sys_id = gel('sysparm_item_guid').value;

//var sys_id = g_form.getValue('sysparm_item_guid');

var sys_id =   g_form.getUniqueValue();

var gr = new GlideRecord('sys_attachment');

//gr.addQuery('table_name','sc_cart_item');

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('myfile') != -1){//meaning if you find a file with xls extension

}

}

}

1 ACCEPTED SOLUTION

Hi Ankur,



I am able to run the code but instead of calling it through another function, I am directly writing the above code.


However, this code doesn't work in Geneva. g_form.getUniqueValue() doesn't return anything in Geneva whereas it works fine in Istanbul. Now my personal instance is on Istanbul but my client's instance is on Geneva.



Also, what I observed is that when I insert attachment on my catalog item, record is not cretaed in sys_attachment table (for geneva), this is the reason below code never works in Geneva whereas in Istanbul as soon as I insert attachment, it creates a record in sys_attachment table. Thoughts???





function validateExtension(){


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('.xls') != -1 || gr.getValue('file_name').indexOf('.pdf') != -1||gr.getValue('file_name').indexOf('.txt') != -1||gr.getValue('file_name').indexOf('.docx') != -1||gr.getValue('file_name').indexOf('.doc') != -1)


{//meaning if you find a file with xls extension


return true;


}


else{


alert("desired file extension not found");


return false; // Even though I am returning false, script is allowing other extension files. Do I need to pass any parameter while calling the function


}


}


View solution in original post

11 REPLIES 11

Hi Priya,

I am also facing the same issue. Do you the script for fixing the above issue. Please share with me.Thank you! 

Praveen125
Kilo Contributor

Hi Priya,

Did u find any solution to add multiple attachments ? Kindly do let me know if any even I have the same kinda requirement.

warm regards,

Praveen