Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Ankur Bawiskar
Tera Patron
Tera Patron

Sometimes there is a customer requirement to validate file names while using attachment type variable on catalog form.

Example: don't allow some special characters in file name

This article will help you for the same

1) Create onChange catalog client script on Attachment type variable

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    g_form.hideFieldMsg('variableName');
    var ga = new GlideAjax('ValidateAttachmentFileName');
    ga.addParam('sysparm_name', 'checkFileName');
    ga.addParam('sysparam_attSysId', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer.toString() == 'invalid') {
            g_form.showFieldMsg('variableName', 'Invalid file name', 'error');
        }
    });
}

2) Create Script Include: It should be client callable

var ValidateAttachmentFileName = Class.create();
ValidateAttachmentFileName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkFileName: function() {

        var badChars = /[%$@()&<>'"]/;
        var sysId = this.getParameter('sysparam_attSysId');
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("sys_id", sysId);
        gr.query();
        if (gr.next()) {
            if (badChars.test(gr.file_name.toString()))
                return 'invalid';
            else
                return 'valid';
        }
    },

    type: 'ValidateAttachmentFileName'
});

Output: Working fine

AnkurBawiskar_0-1764748174710.gif

 

 

Version history
Last update:
2 hours ago
Updated by:
Contributors