Validate uploaded attachment to allow only the provided Excel template in Employee Service Center (E

girishbs06
Tera Expert

Hi everyone,

I'm working on a ServiceNow catalog item in the Employee Service Center (ESC) where users download an Excel template, fill it in, and upload it back as an attachment.

My requirement is to allow users to upload only the provided template and reject any other Excel file.

1 REPLY 1

Tanushree Maiti
Tera Patron

Hi @girishbs06 

 

Try this:

1: Restrict File Extensions

  • Navigate to your Catalog Item and open the Variables related list.
  • Open your Attachment type variable.
  • In the Type Specifications tab, locate the Variable attributes field.
  • Enter the following attribute: allowed_extensions=xls;xlsx.
  • Click Update.

 

 2: Add Exact Name Validation using Client Script

  • Type: onSubmit
  • UI Type: All

 

function onSubmit() {

    var allowedTemplateName = "employee_template.xlsx";     // Replace with your exact provided template file name

    var attachmentSysId = g_form.getValue('your_attachment_variable_name'); // replace 'your_attachment_variable_name' with your actual variable name

        if (attachmentSysId == '') {

        g_form.addErrorMessage("Please attach the completed Excel template before submitting.");

        return false;

    }

    var ga = new GlideAjax('validateAttachmentUtils');

    ga.addParam('sysparm_name', 'validateFileName');

    ga.addParam('sysparm_attachment_id', attachmentSysId);

    ga.addParam('sysparm_allowed_name', allowedTemplateName);

    var response = ga.getXMLWait();

    var result = response.documentElement.getAttribute("answer");

        if (result == 'false') {

        g_form.addErrorMessage("You have uploaded the wrong file. Please download, fill out, and upload the provided template.");

        return false;

    }

}

 

3: Create the Server-Side Script Include

  • Go to System Definition > Script Includes.
  • Create a new Script Include named FileAttachmentUtils
  • Client callable :checked

 

var validateAttachmentUtils = Class.create();

validateAttachmentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

        validateFileName: function() {

        var attachmentId = this.getParameter('sysparm_attachment_id');

        var allowedName = this.getParameter('sysparm_allowed_name');

                var attachGr = new GlideRecord('sys_attachment');

        if (attachGr.get(attachmentId)) {

            if (attachGr.getValue('file_name').toLowerCase() === allowedName.toLowerCase()) {

                return 'true';

            }

        }

        return 'false';

    },

    type: 'validateAttachmentUtils'

});

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti