- 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 04:32 AM
Hi Ks,
Are you using scoped app? If yes then gel() won't work in scoped app
If using scoped app then have this code in Global Scope and test it once.
Also in the if condition check for all the file extensions
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('.pdf') != -1){//meaning if you find a file with xls extension
return true;
}
else{
// meaning desired file extension not found
return false;
}
}
}
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-27-2017 05:44 AM
Thanks, Ankur. Above script is working fine. However, I have already written on script onSubmit to validate attachment number. I wanted to merge both the script.
function onSubmit() {
var sys_id = g_form.getValue('sysparm_item_guid');
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name','sc_cart_item');
attachment.addQuery('table_sys_id', sys_id);
attachment.query();
var numberOfAttachments = attachment.rows.length;
if (!attachment.next()) {
alert("You must attach a invoice to submit.");
return false;
}
else if(numberOfAttachments >1){
var confirmBox = confirm("Only one invoice allowed, do you want to replace the existing one?");
if(confirmBox.toString() == 'true'){
return false;
}
else{
return false;
}
}
validateExtension();
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 05:48 AM
Hi Ks,
Is the script getting executed till that line where there is else{} condition shown in bold.
If the script is being returned with true before that line of code then code won't reach the else condition
Which test case scenario is failing in your case?
Can you add alert at few of the places in the second method
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
09-27-2017 06:06 AM
The script is getting executed till that line where there is else{} condition shown in bold.
so, I am checking if there is one attachment >> If yes, then I am checking for its extension >> If the extension is wrong, I am showing alert and returning false.
I am getting the alert that extension is not correct, but still, catalog item is creating a request.