- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 12:07 AM
Hi All,
I have written a client script to restrict attachment file types.I want to allow users to attach only .csv file types.
written code for both portal and non-portal, On portal its working as expected indexOf returns different value for all csv attachments.
On non-portal indexOf returns -1 for all type of attachments. (jpg,pdf,xml.csv..)
function onSubmit() {
try
{
//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;
var rowcount=0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
while (attachment.next()) {
rowcount++;
}
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && attachment.content_type.indexOf(".csv") <= -1){
alert(attachment.content_type);
alert("Attachment should be in only csv format!");
return false;
}
else return true;
}
catch(e)
{
//for-portal
if(this.document.getElementsByClassName('get-attachment').length ==1) {
var value = (this.document.getElementsByClassName('get-attachment')[0].innerHTML).toLowerCase();
alert(value.indexOf('.csv'));
if ((value.indexOf('.csv')>-1)){
return true;
}
else
{
alert ("Attachment should be in only csv format!");
return false;
}
}
else if(this.document.getElementsByClassName('get-attachment').length <1)
{
alert( "Attachment Required!!");
return false;
}
}
Is there anything wrong in my code?Need some suggestion.
Thanks in Advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 12:13 AM
Hi Sana,
what content_type you are getting?
Did you alert that?
because content_type could be application/csv or application/vnd.ms-excel for a csv file
why not check file name itself i.e. filename contains .csv?
Also you can get the rowcount in client side and no need to use counter to increment in while loop
try{
//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;
alert('sysid is ' + sysid); // is this coming properly
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
var rowCount = attachment.rows.length;
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && attachment.content_type.indexOf(".csv") <= -1){
alert(attachment.content_type);
alert("Attachment should be in only csv format!");
return false;
}
else return true;
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 12:34 AM
instead of content type, you can also use file name.
var fileName = attachment.file_name
if(rowcount == 1 && fileName.indexOf(".csv") <= -1){
alert(fileName);
alert("Attachment should be in only csv format!");
return false;
}
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2021 07:12 AM
It worked! Thanks for your help!!