Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Ankur Bawiskar
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:
‎12-03-2025 02:33 AM
Updated by:
Contributors