Require minimum attachment size in portal on catalog item

Hannah C
Giga Expert

Hello!

I have an onSubmit client script that prevents a user from submitting the catalog item without an attachment, but is there a way to require a minimum file size? Before they can submit this catalog item in the portal I want it to make sure they have a minimum file size of 65 KB.

4 REPLIES 4

sachin_namjoshi
Kilo Patron
Kilo Patron

You will have to use GlideAjax to query sya_attachment to determine size of file and provide validation message to user.

Your script include will return flag value based on attachment size.You can use below sample code in your script include

 

var grInc = new GlideRecord('sys_attachment);


grInc.addQuery('table_name',"incident");


grInc.addQuery('table_sys_id',current.sys_id);


grInc.Query();


var totalSize=0;


while(grInc.next()) {


         totalSize+= parseInt(grInc.size_bytes);


}


if(totalSize+parseInt(current.size_bytes) > <your Size Limit>) {


         gs.addInfoMessage("Attachment size exceeded");


         current.setAbortAction(true);


}

 

Regards,

Sachin

Hi thank you for the response! I'm a little confused...won't the attachment not be on the sys_attachment table yet though because they haven't submitted it yet?

You don't have to save record to submit attachment with catalog item.

You will see below kind of record in sys_attachment table after you attach file catalog item.

 

find_real_file.png

 

Regards,

Sachin

Ok I started building the scripts based on your suggestions but need some help as it is not working. Please let me know if you have any suggestions!!

 

Script Include:

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

checkAttachment : function() {

var gr = new GlideRecord('sys_attachment');
gr.addQuery ('table_name', 'sc_cart_item');
gr.addQuery ('table_sys_id', current.sys_id);

gr.query();

var totalSize = 0;
while (gr.next()){
totalSize += parseInt (gr.size_bytes);
}

if (totalSize+parseInt(current.size_bytes < 65)){
return false;
}

else
{
return true;
}

}
});

 

Client Script (onSubmit on the catalog item):

function onSubmit() {

function ValidateAttachment(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true'){
return;
}

if(g_form.getValue('sbg') == 'aero' && answer == 'false'){
alert('You must download, complete, and attach the User Security Request form before submitting this request. Please make sure all of the required fields are populated.');
return false;
}

}
var ga = new GlideAjax('AttachmentSize'); //Name of Script Include
ga.addParam('sysparm_name', 'checkAttachment'); //Name of the function in the script include
ga.getXML(ValidateAttachment);


}