Calculate specific table attachments size from sys_attachment table

Sumanth16
Kilo Patron

Hi,

I need to calculate total attachments size of hr table from sys_attachment table ? Can anyone  help me with the script 

 

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name','hr_case');
gr.Query();
var totalSize=0;
while(gr.next()) {
totalSize=parseInt(gr.size_bytes);

}

gs.print(totalSize);

5 REPLIES 5

xiaix
Tera Guru

Total sys_attachment size for all tables:

(function(){
	function convertBytes(bytes) {
		var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
		if (bytes == 0) return '0 Byte';
		var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)),10);
		return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
	}
	var r = 0;
	var att = new GlideAggregate('sys_attachment');
	att.addAggregate('SUM', 'size_bytes');
	att.query();
	while (att.next()) {
		r += parseInt(att.getAggregate('SUM','size_bytes'),10);
	}
	gs.print(convertBytes(r));
})();

 

Total sys_attachment size for hr_case:

(function(){
	function convertBytes(bytes) {
		var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
		if (bytes == 0) return '0 Byte';
		var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)),10);
		return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
	}
	var r = 0;
	var att = new GlideAggregate('sys_attachment');
	att.addAggregate('SUM', 'size_bytes');
	att.addQuery('table_name', 'hr_case');
	att.query();
	while (att.next()) {
		r += parseInt(att.getAggregate('SUM','size_bytes'),10);
	}
	gs.print(convertBytes(r));
})();