Restrict user from submitting attachment other than pdf file

Sakeena Fathim1
Tera Expert

Hi Team,

Greetings!!

I have a requriment where i need to restrict a user to upload a document other than PDF like xslx,xml,txt. User wants only pdf file to be upload for one specific catalogue item. I went through the community but unforunently i didn't find such article. If anyone knows about it please lemme know thanks in advance.

Note: Onsubmit client script I can not do glide record on "sys_attachment" table as best practice by my client. 

Thanks

Sakeena Fathima

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I have provided solution for this earlier; enhance it

Service Catalog - PDF Attachment

Note: It used DOM manipulation which is not recommended

Regards
Ankur

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

Hi Ankur, 

Greetings!!

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var arr = [];
    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
        var pdfFound = 'false';
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].indexOf('.pdf') > -1) {
                pdfFound = 'true';
                break;
            }
        }

        if (pdfFound == 'false') {
            alert("You must attach a PDF file");
            return false;
        } 

    }
}

I written this code working fine like user need to upload a file with .pdf file but if user not upload a file and try to submit a record alert message is coming.. my client don't want that alert if user try to submit a request without file it should allow user to submit a request... where i need to change the script can you please guide me here... 

Please find a attachment for your reference

Hi,

so attaching file is not mandatory but if file is attached then it should be only PDF?

If yes then update as this

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var arr = [];
    if (window == null) {
        // portal
        var z = this.document.getElementsByClassName("get-attachment ng-binding ng-scope");
        var k;
        if(z.length == 0)
            return true;
        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
        var pdfFound = 'false';
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].indexOf('.pdf') > -1) {
                pdfFound = 'true';
                break;
            }
        }

        if (pdfFound == 'false') {
            alert("You must attach a PDF file");
            return false;
        } 

    }
}

Regards
Ankur

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

Hi Anukar,

 

The above code functions effectively on the ServiceNow portal, yet encounters issues when a user submits the catalog item in the Service Catalog within ServiceNow. Attached is the error message for reference. Can you adjust the code to ensure consistent functionality for users submitting catalog items within the Service Catalog?

 

Thank you,

Melissa

Hi Anukar,

The code functions effectively on the ServiceNow portal, yet encounters issues when a user submits the catalog item in the Service Catalog within ServiceNow. Attached is the error message for reference. Can you adjust the code to ensure consistent functionality for users submitting catalog items within the Service Catalog?

 

Thank you,

Melissa