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

Rajesh Mushke
Mega Sage
Mega Sage
Hey Sumanth, Below example may helpful to you. FIND TOTAL ATTACHMENT SIZE: Run a script to find the total size of all the attachments in your ServiceNow database. With some new ServiceNow contracts requiring the ServiceNow database to be under 4TB, this is something you may need to inspect before your contract renewal. TOTAL ATTACHMENTS SIZE SCRIPT 1. Elevate your privileges to security admin 2. In scripts - background run this script findAttachmentsSize(); function findAttachmentsSize(){ var size_compressed = 0; var size_bytes = 0; grAttachment = new GlideRecord('sys_attachment'); grAttachment.query(); gs.print('Total Number of Attachments: ' + grAttachment.getRowCount()); while (grAttachment.next()){ size_compressed += grAttachment.size_compressed; size_bytes += grAttachment.size_bytes; } gs.print('Total Size Compressed (bytes): ' + size_compressed); gs.print('Total Size Bytes (bytes): ' + size_bytes); } If you haven't setup the max attachment size, you may want to do so as well. ServiceNow Wiki - Set Max Attachment Size You may be surprised at some of the file sizes of attachments that users have uploaded! Let me know. if you need more help Thanks, Rajashekhar Mushke Community Leader -18


Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Rajesh Mushke
Mega Sage
Mega Sage

Please Refer below links:

FIND TOTAL ATTACHMENT SIZE

Administering in attachments

 

Thanks,

Rajashekhar Mushke

Community Leader -18



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Sumanth16
Kilo Patron

It is writing some lengthy number and it is very hard to convert into kb even.I want function to convert into kb in the code

JFG
Kilo Explorer

Adding 'parseInt' to avoid var CONCATENATION instead of SUM

Adding conversion to MB instead of BYTES

(tested in Madrid)

---------------------------------------------------------------------------------------------------

findAttachmentsSize();
function findAttachmentsSize(){
var size_compressed = 0;
var size_bytes = 0;
grAttachment = new GlideRecord('sys_attachment');
grAttachment.query();
gs.print('Total Number of Attachments: ' + grAttachment.getRowCount());
while (grAttachment.next()){
size_compressed += parseInt(grAttachment.size_compressed);
size_bytes += parseInt(grAttachment.size_bytes);
}
gs.print('Total Size Compressed (MB): ' + parseInt(size_compressed / 1024 / 1024));
gs.print('Total Size Bytes (MB): ' + parseInt(size_bytes / 1024 / 1024));
}

---------------------------------------------------------------------------------------------------