Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Restrict file attachment types in single catalog item

MonicaW
Tera Guru

I'm trying to figure out how to write an onSubmit script to restrict attachment types to the following file types in a single catalog item using the built-in attachments icon (not the variable):

pdf,zip,txt,doc,docx,dwg,gif,csv,xls,xlsx,ppt,xml,xsl,bmp,html,png,pptx,cfr,jpeg,mp4,mov,odt,ods,odp,jpg,jpeg,rtf,wmv,wm,wma,heic,msg,jfif

 

I have found some older posts here but they don't seem to address multiple file types.

Thank you for any help.

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@MonicaW 

then no direct way available but you need to use DOM manipulation

I used something similar in past and it worked fine for me.

It gave me array of file names in both native + portal

Based on that you can enhance the logic

Ensure "Isolate Script" field is marked as False for your client script

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var arr = [];
    var hiddenVariable = g_form.getValue('hidden_variable');

    try {
        if (window == null) {
            // portal
            var z = this.document.getElementsByClassName("get-attachment ng-binding ng-scope");
            var k;
            for (k = 0; k < z.length; k++) {
                var value = z[k].innerHTML;
                value = value.substring(0, value.indexOf('('));
                arr.push(value.trim());
            }
            // now check if the allowed file name is present in the array or not
        }
    } catch (ex) {
        // native get all the file names native
        $j("a.content_editable").each(function(index) {
            var val = $j(this).text();
            arr.push(val);
        });
        // now check if the allowed file name is present in the array or not
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MonicaW 

it gave me file names in portal

I believe you can take it further from here.

AnkurBawiskar_0-1762525322533.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader