restrict the form to submit when attached documents are not in these formats( .pdf, .docx)

Ramesh_143
Giga Guru

I have a catalog item, when the user attaches any document while filling catalog form do not allow the form to submit, if the attached document are in these formats pdf, docx. 

 

Anyone help me how to fetch attached document names, format and to achieve the above thing.  I want to Perform this in Portal Side. If user attaches other than these documents show some error or alert message.

2 ACCEPTED SOLUTIONS

Aditya02
Tera Guru

hi @Ramesh_143 

 

you can achieve your functionality by writing an onSubmit() script for the form. Here is the script:

 

function onSubmit() {

    var a = this.document.getElementsByClassName('get-attachment');

    var b = a.length;

    //alert(b);

    if (!b) {

        g_form.addErrorMessage('You must add an attachment');

        return false;

    } else {

        for (var i = 0; i < a.length; i++) {

            var t = a[i].title;

            var fileExtension = t.split('.').pop().toLowerCase();

            if (fileExtension != 'docx' && fileExtension != 'pdf') {

                g_form.addErrorMessage(fileExtension + ' ' + 'You must add file types of docx and xlsx only');

                return false; // Prevent form submission

            }

        }

        return true;

    }

}

 

 

  • Here When I attached a document it will check weather it is in xlsx or docs or not. For Example, I am attaching Text document then it will give an error message.
  • The Below Client side Script works when the form is viewed on the ServiceNow Portal. It involves Manipulating the DOM (Document Object Model) Events.
  • Direct DOM access is not Possible on the ServiceNow Platform(Backend) using client-side scripts that run in users browser.

If my solution gets resolved your problem, Please give me a like and make my answer as 'correct answer'.

 

Thanks ,

Aditya

View solution in original post

VishaalRanS
Tera Guru

Hi @Ramesh_143 

 

To restrict document attachments in a ServiceNow catalog item to only specific formats (PDF and DOCX) on the portal side, you can use a client script to validate the file types before submission.

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

Anantha27
Mega Guru

Hi @Ramesh_143 

There will be one OOB feature for enabling button in ServiceNow if it gets enabled you can see the output.
Thank you