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.

Not Allow Special Characters in the attachment type variable in the Service Catalog

D V Sandeep
Tera Contributor

Hi Team,
Can we restrict special characters (% $ @ ( )  &  <   >  ' ") in the attachment type variable?
@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@D V Sandeep 

This is the working solution which worked for me fine

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');
        }
    });
}

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

validate file name in attachment type variable.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

14 REPLIES 14

Hi @Sarthak Kashyap 
Thanks for your response. I checked your solution, but it is still not working for me. The system is still allowing files with special characters in the name to be attached. Please check the attached screenshot.
FYI, I have also used the variable attribute 'allowed_extensions=csv' for this attachment type variable.
Please check the below client script:

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

    alert("Called = " + g_form.getValue('final_attachment'));

    var attachmentWidget = g_form.getControl('final_attachment');
    alert("attachmentWidget = " + JSON.stringify(attachmentWidget));
    var fileName = attachmentWidget.displayValue();
    var invalidChars = /[%$@()&<>'"]/;

    if (invalidChars.test(fileName)) {
        alert("Your file name have invalid characters please remove and add");
        g_form.clearValue("final_attachment");
        return false;
    }

}

Hi @D V Sandeep , Did you check the console what error it is show ? 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@D V Sandeep 

why are you concerned about file name in attachment type variable?

you can write onChange catalog client script on that Attachment type variable and use GlideAjax to validate it

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur Bawiskar
Tera Patron
Tera Patron

@D V Sandeep 

This is the working solution which worked for me fine

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');
        }
    });
}

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

validate file name in attachment type variable.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 
When I am uploading the attachment in the attachment type variable, I am getting the below error.

DVSandeep_0-1764757623944.png