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

SanjivMeher
Mega Patron
Mega Patron

Are you talking about special characters in attachment name?

Can you elaborate more about your use case?


Please mark this response as correct or helpful if it assisted you with your question.

Sarthak Kashyap
Mega Sage

Hi @D V Sandeep ,

 

I check for your problem in my PDI and it works for me, I checked for name in file name please check solution below 

 

I created a onChange client script on attachment variable and add below code

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

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

    var attachmentWidget = g_form.getControl('add_here');
    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("add_here");
		return false;
	}

    }

SarthakKashyap_0-1764664387302.png

 

Result

 

My file has special characters

SarthakKashyap_1-1764664453279.png

 

SarthakKashyap_2-1764664467292.png

SarthakKashyap_3-1764664478111.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regard,

Sarthak

D V Sandeep
Tera Contributor

Hi @SanjivMeher 
When we try to attach a file to the attachment type variable in a record producer, the system should prevent the file from being attached if the file name contains special characters.

Hi @D V Sandeep ,

Did you check my solution ? I also did for file name only.