- 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
09-27-2017 06:09 AM
Hi Ks,
So you are saying that the code is getting inside else condition and alert being shown but return false is not stopping the form submission.
Can you check whether there is any other onSubmit script which is submitting the form.
Ideally return false should stop the form submission.
Mark Correct if this solves your issue and also hit Like and 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
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
09-28-2017 02:55 AM
Hi Ks,
If whatever you are telling about attachment not getting attached as soon as you attach the file to catalog item in the sys_attachment table then this is something to do with the instance issue. Because ideally this should create a record in sys_attachment table.
I would recommend to raise a hi ticket with servicenow.
Mark Correct if this solves your issue and also hit Like and 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
03-23-2023 10:40 PM
Hi Ankur,
function onSubmit() {
//Type appropriate comment here, and begin script below
var sys_id =g_form.getUniqueValue();
alert('sysid'+sys_id);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sys_id);
gr.query();
if (gr.next()) {
if (gr.getValue('file_name').indexOf('.jpg') != -1 || gr.getValue('file_name').indexOf('.pdf') != -1 || gr.getValue('file_name').indexOf('.png') != -1|| gr.getValue('file_name').indexOf('.msg') != -1) {
return true;
} else {
g_form.addErrorMessage('Please attach allowed extensions.Allowed attachment extensions: jpg, png, pdf, msg');
return false;
}
}
}
g_form.getUniqueValue() is returning the sys_id of the catalog item and it is not entering into inside if part.
Is there any way to restrict the attachments using on submit client script for a catalog.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2020 10:25 AM
its working fine for single attachment , if i use more than 2 attachments its not working .
Kindly correct this code to restrict for 5 attachments ( all 5 attachments should be pdf, docx, txt, xls, xlsx formats)