- 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
11-13-2020 09:54 AM
Hi Ankur, I am trying to do same but allowing user to enter .txt file only. Could you please check my code why its not working? it is accepting all the files.
var sysid =g_form.getElement('sysparm_item_guid').value;
alert(sysid );
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;
var file = attachment.file_name; // this is not working
alert(rowcount);
alert(file); // alert is not working
alert(attachment.content_type) // this is coming as undefined
if(rowcount==0 )
{
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && attachment.content_type.indexOf(".txt") >= -1){
alert("Attachment should be in only txt format!");
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2020 11:12 PM
It is not recommended to use GlideRecord in client script
Regards
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
07-19-2021 03:19 AM
Hi ankur needs to add only csv file but....... attachment.content_type.indexOf(".csv") <= -1) this is returning -1 or csv,txt file also ..please help me with that
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2021 03:54 AM
Can you please post a new question for this as this thread is already answered.
Please tag me in that question and share all the details
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:29 AM
Issue is in code after while loop.
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==0){ // Missing {}
alert("Attachment Required!!");
return false;
}
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;
}
}
}
Thank you,
Ali