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

@D V Sandeep 

there is something wrong in your client script.

I already shared a working solution with you

share your scripts and screenshots of that script here

💡 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 
I used the same solution which you shared here, please check the below screenshot.
FYI, I also used 'allowed_extensions=csv' variable attribute for this variable.
Client Script (Customer Service Application Scope)

DVSandeep_0-1764758365884.png

Script Include:

DVSandeep_1-1764758516194.png

 

@D V Sandeep 

any reason you created script include in global scope even though catalog item is in CSM scope?

client script is in which scope?

Both client script + script include should be in same scope as that of Record Producer i.e. Customer Service

Once you do that it will work fine. for my example my catalog item was in global scope hence I created Script include in global scope

💡 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 ,

At first I tried with the same scope (customer service) for both scripts, it gave me the same error so I just created a script include in global to check but unfortunately I got the same error. 
I am getting the below two errors when I created script include in customer service:

DVSandeep_0-1764761341648.png

 



@Ankur Bawiskar 
The issue was with the AbstractAjaxProcessor base class in the Script Include, and I resolved it by referencing global.AbstractAjaxProcessor.
Thank you for your help.