How to limit the number of attachments on Catalog Item?

Sam Jae
Tera Contributor

Hi, 


I need to limit the end customer to being only able to add up to 5 attachments on a catalog item. How do I go about that? Also, each attachment cannot be more than 10mb. Please help me 

1 REPLY 1

Anantha Gowrara
Kilo Sage

Hi @Sam Jae You restrict number of attachments and attachment size through script. You can create one script include and add logic(sample code) and call that SI from catalog client script.

Sample code

var AttachmentValidation = Class.create();
AttachmentValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateAtatchment: function() {
        var incidentId = this.getParameter('sysparm_ID');
        var grInc = new GlideRecord('sys_attachment');
        grInc.addQuery('table_name', "incident"); //change table name
        grInc.addQuery('table_sys_id', incidentId);
        grInc.query();
        var totalSize = 0;
        while (grInc.next()) {
            var fileList = [];
            var totalCount = grInc.getRowCount();
            totalSize += parseInt(grInc.size_bytes);
            if (totalSize > 10000)
                fileList.push(grInc.file_name);
        }
        //return fileList.length;
        if (fileList.length > 0)
            return "Below Files exceed max size limit.Please remove these files to proceed further " + fileList.toString();
        if (totalCount > 1)
            return "You cannot include more than 1 attachment.Delete uncessary attachments to proceed further";

    },

    type: 'AttachmentValidation'
});

 

Client Script

function onSubmit() {
    //Call script include
    var ga = new GlideAjax('global.AttachmentValidation'); //Scriptinclude
    ga.addParam('sysparm_name', 'validateAtatchment'); //Method
    ga.addParam('sysparm_ID', g_form.getUniqueValue()); //Parameters
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        alert(response);
    }

}