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

Leo Ruggiero
Tera Contributor

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.

18 REPLIES 18

Interesting, because ANkur's solution is use g_form.getUniqueValue() in your CCS

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Yeah, https://www.servicenow.com/community/developer-forum/is-there-a-way-to-get-the-cart-item-id-in-porta...

This resolved the issue for me. Once I added g_form.generatedItemGUID to the Catalog Item widget and swapped the instance page editor to the new catalog item widget and then had my client script call  g_form.generatedItemGUID, i now get the table_sys_id

Still have a lot more to fix up though, so in the process of working on that now since I don't think the rest of the script still works.

I’m seeing a couple of things that could be issues.  I’m assuming that you’ve confirmed that your query is getting the necessary data.

 

File_name in sys_attachment is a string, so you don’t need to use getValue(), direct assignment (var filename = gr.file_name) will work.

 

Your test for special characters is running backwards (regular expressions is not one of my strong points).  I set up a simple test script (ran it in Background Scripts)

var fileName = "filename";
var specialChars = /^[a-zA-Z0-9]+$/;
gs.print("theResult is " + specialChars.test(fileName))

That returned true.  If I put an underscore between file and name, or add other special characters, I get false.  Does your other process reject file names that have underscores in them?

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

So, with the script below you can see I tried setting the variables from the glide record, but everything comes back as undefined. However if we look at the cat_id, we can see that it does indeed return the correct table_sys_id provided even in the screenshot below from the sys_attachment table.

It moves forward to the !gr.next and says we must attach an attachment, even though we can see clearly there is an attached record, and the glide record query really should pull a record there, i'm not sure why it isn't...

function onSubmit() {
    var cat_id = g_form.generatedItemGUID;
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "sc_cart_item");
    gr.addQuery("table_sys_id", cat_id);
    gr.query();
    var tableName = gr.table_name;
	var tableSysID = gr.table_sys_id;
	var fileName = gr.file_name;
	alert('Table Name: ' + tableName + ' Table SysID: ' + tableSysID + ' File Name: ' + fileName + ' cat_id variable: ' + cat_id);
    if (!gr.next()) {
        alert("You must add an attachment before submitting this request.");
        return false;
    }

 

LeoRuggiero_0-1674237140010.png

LeoRuggiero_1-1674237166529.png