- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2017 04:22 AM
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.
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
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2017 02:06 AM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2021 05:54 AM
Hi Priya,
I am also facing the same issue. Do you the script for fixing the above issue. Please share with me.Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2021 03:48 AM
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