Restrict attachments size on single catalog item

MonicaW
Tera Guru

I have a catalog item where we want to restrict the size of all attachments to 12 MB total.  I tried using the attachment variable, but it only allows the selection of ONE attachment.  I want the ability to add multiple attachments using the built-in "add attachments" on the catalog item itself and have it calculate the size of all of them and restrict it to 12 MB.

 

I found the following code that I put in an onSubmit script, but it's not working - perhaps I need to change more elements than the var maxAttachmentSizeMB = 12; line?

>>>>>>>>>>>>>>>>>>>>>>>>>

function onSubmit() {
var attachments = this.angular.element("#sc_cat_item").scope().attachments; // Adjust element ID as needed
var submitForm = false;
var maxAttachmentSizeMB = 12; // Set your desired limit in MB

attachments.forEach(function(attachment){
var sizeBytes = parseInt(attachment.size_bytes);
var sizeMB = sizeBytes / (1024 * 1024); // Convert bytes to MB.
var sizeString = String(attachment.size_bytes); // Get the size as a string to include the units
alert(sizeString);

if(sizeMB > maxAttachmentSizeMB){
alert(attachment.file_name + ' is too large. Maximum allowed size is ' + maxAttachmentSizeMB + ' MB.');
submitForm = false;
}
});
return submitForm;
}

20 REPLIES 20

Rafael Batistot
Kilo Patron

Hi @MonicaW 

 

You’re very close, but there are a couple of issues with your script:

 

  1. You’re checking each attachment individually, but you want the total size of all attachments.
  2. submitForm is initialized as false — so even if everything is fine, your form won’t submit. You need to start with true and only block when the limit is exceeded.
  3. The Angular reference (#sc_cat_item) may not always resolve properly, depending on portal vs platform UI. Sometimes you need to use g_form APIs instead.
  4. You’re alerting size in raw bytes, but that won’t help the end user.

Here’s a cleaned-up version you can try as a catalog client script → onSubmit:

 

function onSubmit() {
// Max size in MB
var maxAttachmentSizeMB = 12;
var maxBytes = maxAttachmentSizeMB * 1024 * 1024;

// Get all attachments for this form
var attachments = this.angular.element("#sc_cat_item").scope().attachments || [];

var totalSize = 0;

attachments.forEach(function(attachment) {
totalSize += parseInt(attachment.size_bytes || 0);
});

if (totalSize > maxBytes) {
alert("Total attachment size exceeds " + maxAttachmentSizeMB + " MB. " +
"Please remove some files before submitting.");
return false; // Block submission
}

return true; // Allow submission
}



Notes:

  • return false will stop submission.
  • This checks total size, not per-file size.
  • If you want both (e.g., per file < 10 MB, total < 12 MB), you can add another check inside the loop.
  • Make sure this is a Catalog Client Script → type: onSubmit attached to your item.
If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Hi Tera:

Thank you so much - as you can tell I'm a novice at scripting.  I tried the cleaned up script you provided but it didn't work.  However, the javascript error I was getting on submission went away and the Submit step takes a little longer - like it's trying to calculate the size, but then it submits fine.  Thank you for your suggestion.

Hi again Tera:

I should mention that I attached a 17MB file to test it (12MB is limit).

Hi @MonicaW 

Sorry delay, i've used onSubmit + script include to get this solution. See steps

1 - Create Script include

RafaelBatistot_0-1757613586767.png

 

Script 

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


    checkAttachmentSize: function() {

        var attId = '';

        var maxSizeKB = 100; // KB limit
        var maxSizeBytes = maxSizeKB * 1024;

        var att = new GlideRecord('sys_attachment');
        att.addQuery('content_type', 'text/plain'); // i've use txt format to test 
        att.addQuery('table_name', 'sc_req_item');
        att.orderByDesc('sys_created_on');
        att.query();
        if (att.next()) {
            attId = att.sys_id; // Id of attach sent
        }

        var gr = new GlideRecord("sys_attachment");
        gr.get(att.sys_id);

        var size = gr.getValue("size_bytes");

        if (size > maxSizeBytes) {
            return false; 
        }

        return true;
    },

    type: 'ValidateAttachmentAjax'
});


In your catalog you create a Catalog Client Scripts -> onSubmit

function onSubmit() {

    var answer = "";
    var ga = new GlideAjax('ValidateAttachmentAjax');
    ga.addParam('sysparm_name', 'checkAttachmentSize');
    ga.getXML(updateCampus);

	g_form.addInfoMessage('size exceed');
	return false;

    function updateCampus(response) {
        answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == 'false') {
            return false;
        }
    }
}


Once you submit the catalog if exceed it will abort the operation

RafaelBatistot_1-1757614189641.png

 





If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.