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.

Validating an attachment variable

Vignesh Raman
Tera Contributor

How can I validate the type (xls/xlsm) and size (<5 MB) in an attachment variable and output an error for the same?
Can you write an onSubmit client script for the same?

1 ACCEPTED SOLUTION

Harish_K07
Giga Guru

@Vignesh Raman - If you're using a variable of attachment type, use the following onChange script -

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ajax = new GlideAjax('Newglidingabc');
    ajax.addParam('sysparm_name', 'checkAttachmentSize');
    ajax.addParam('sysparm_existing', newValue);
    ajax.getXML(getRITMdata);

    function getRITMdata(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            alert(answer);
            g_form.clearValue("add_attachment"); //give your attachment variable name
        }
    }
}

 

Script include -

checkAttachmentSize: function() {
        var grA = new GlideRecord("sys_attachment");
        grA.addQuery("sys_id", this.getParameter("sysparm_existing"));
        grA.query();
        if (grA.next()) {
                    if (grA.getValue("size_bytes") > 5000) {
                return "File size must be less than 5MB.";
            } else if (grA.getValue("content_type") != "xls" && grA.getValue("content_type") != "xlsm") {
                return "Please attach xls or xlsm files";
            }
        }
    },

View solution in original post

4 REPLIES 4

Vengadesh
Tera Guru

Hi @Vignesh Raman,

 

Did you created a separate variable called "Attachment" using attachment variable type or else using the OOTB attachment icon ?

Harish_K07
Giga Guru

Hi @Vignesh Raman 

 

If you're trying to achieve the file type validation using the OOTB attachment option, please refer to the following video explanation on achieving this - https://www.youtube.com/watch?v=EUdkUfm0OGM

 

Best Regards,

Harish

Harish_K07
Giga Guru

@Vignesh Raman - If you're using a variable of attachment type, use the following onChange script -

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ajax = new GlideAjax('Newglidingabc');
    ajax.addParam('sysparm_name', 'checkAttachmentSize');
    ajax.addParam('sysparm_existing', newValue);
    ajax.getXML(getRITMdata);

    function getRITMdata(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            alert(answer);
            g_form.clearValue("add_attachment"); //give your attachment variable name
        }
    }
}

 

Script include -

checkAttachmentSize: function() {
        var grA = new GlideRecord("sys_attachment");
        grA.addQuery("sys_id", this.getParameter("sysparm_existing"));
        grA.query();
        if (grA.next()) {
                    if (grA.getValue("size_bytes") > 5000) {
                return "File size must be less than 5MB.";
            } else if (grA.getValue("content_type") != "xls" && grA.getValue("content_type") != "xlsm") {
                return "Please attach xls or xlsm files";
            }
        }
    },

Vignesh Raman
Tera Contributor

Thanks for your response. Apologies for the delay in my response!