How to check for attachment and for special characters or multiple instances of a file extension?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 06:01 AM - edited ‎01-20-2023 06:21 AM
Hey all, I'm having a few difficulties checking for attachments, checking the attachments name for special characters, and checking for multiple instances of file extensions. We need to check for this due a process we have that errors out if the file name has these things. Here is the code I've put together but can't seem to get any of it working right.
Running this from a catalog client script so that this should work from the portal view.
function onSubmit() {
var cat_id = g_form.getValue('sysparm_item_guid');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must add an attachment before submitting this request.");
return false;
}
var fileName = gr.getValue("file_name");
var specialChars = /^[a-zA-Z0-9]+$/;
if (specialChars.test(fileName)) {
alert("The file name contains special characters, please remove them and try again.");
return false;
}
var fileExt = fileName.substr(fileName.lastIndexOf(".") + 1);
var multipleExts = new RegExp(fileExt + "{2,}");
if (multipleExts.test(fileName)) {
alert("The file name contains multiple instances of the file extension, please remove them and try again.");
return false;
}
}
Right now the code throws a false on "You must add an attachment", regardless if there is an attachment. However I did have this portion working previously with different code but with the rest of the code it failed to check for the special characters and failed to check for the multiple instances of the file extension.
Anyone have any ideas on how I can go about this? Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 06:15 AM - edited ‎01-20-2023 06:18 AM
Hi Leo,
I'm assuming that the script you are showing is an On Submit for your item. Have you confirmed that any attachments are stored in sys_attachment before the order is fully placed? That strikes me as the only reason for the check to fail. Have you considered putting attachments as part of your form and turning off the paper clip icon?
:{)
Helpful and Correct tags are appreciated and help others to find information faster

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 06:24 AM - edited ‎01-20-2023 06:25 AM
Hi John! Just updated the post, as I forgot to mention that I am running this from a Catalog Client Script.
And yes, I've checked sys_attachment and it is showing the file there before submission. Once it's attached it shows in sys_attachment

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 06:27 AM
Ah... I tried to alert the cat_id and it seems I'm not getting the sys id for the table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 06:40 AM
Hi Leo,
What table is the attachment record showing? If you think about it, the relevant relationship ends up being between sc_req_item and sys_attachment. Have you tried looking in sc_cart_item for the sys_id being displayed? Catalog item storage can be a pain to unwind due to the separations necessary for the various variables, etc.
:{)
Helpful and Correct tags are appreciated and help others to find information faster