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

The attachment is showing the in sc_cart_item table as it should. Though it seems g_form.getValue('sysparm_item_guid'); isn't getting me the sys id. Any ideas why?

 

The catalog client script is not isolated. 

LeoRuggiero_1-1674225767934.png

 

 

THe problem s that you are using sysparm_item_guid.  Take a look at the correct answer in this posting

Hope that helps.

:{)

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

I've followed the workaround for modifying the SC Catalog Item widget, though I'm not sure how to use my new cloned SC Catalog Item Widget on the portal.

Is there any documentation that guides me through this process? So that I can test out this new widget?

Hi Leo,

It looked to me as though all you needed to do was replace the command for getting the sys_id into the CCS.  That shouldn't require a cloned widget.  Normally, the way to see that everything is working correctly with a widget like that would be to replace the base widget with the clone on the appropriate page.

Hope that helps.

:{)

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

Nope, the work around is adding g_form.generatedItemGUID = $scope.data._generatedItemGUID; to the SC Catalog Item widget, then on your client script calling g_form.generatedItemGUID; instead of g_form.getValue('sysparm_item_guid');