We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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.

3 REPLIES 3

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

Aaron _S
Mega Sage

If you only need to validate the name of the attachment, Tanushri Maitis reply should help you.

 

If you also need to validate the form of the Excel template, you could extend the server-side script with a few additional validation functions.

There's the GlideExcelParser API with which you can validate both the header columns and the contents of the file.

Getting header columns via .getHeaderColumns() you could validate that a User hasn't modified existing column names or added their own.

 

A google search should yield a usable AI generated script template for GlideExcelParser.

Ankur Bawiskar
Tera Patron

@girishbs06 

where are they doing it?

from where are they downloading? where they are uploading?

screenshots

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