incident table

renukakandu
Tera Contributor

@I am creating an incident from the record producer with an attachment, and I want to fetch the attach attachment size, if it is greater than 3mb, I need to populate comments in the incident form

 

4 REPLIES 4

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @renukakandu - Depending on how and when the attachment is uploaded, you can do something like this:

 

// Get attachments linked to the producer (current)
var sa = new GlideRecord('sys_attachment');
sa.addQuery('table_sys_id', current.getUniqueValue());
sa.query();

while (sa.next()) {
    var sizeMB = sa.size_bytes / (1024 * 1024);
    if (sizeMB > 3) {
        current.comments = "An attachment larger than 3 MB was uploaded (" +
            Math.round(sizeMB * 100) / 100 + " MB).";
    }
}

 

vignesh parthib
Tera Guru

Hi @renukakandu 

Write a Business Rule on incident table and Condition Insert

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", current.sys_id);
gr.addQuery("table_name", "incident");
gr.query();

var oversized = false;
while (gr.next()) {
var sizeInMB = parseInt(gr.size_bytes) / (1024 * 1024);
if (sizeInMB > 3) {
oversized = true;
break;
}
}

if (oversized) {
current.comments = "Note: One or more attachments exceed 3MB in size.";
current.update(); // Safe in after insert
}

 

Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."

Ankur Bawiskar
Tera Patron
Tera Patron

@renukakandu 

you can use after insert BR on incident table and then query sys_attachment and know the size and accordingly update the comments

Business Rule: After Insert on incident

Script:

(function executeRule(current, previous /*null when async*/) {
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_name', 'incident');
    grAttachment.addQuery('table_sys_id', current.sys_id);
    grAttachment.addQuery('size_bytes', '>', 3145728); // 3 MB in bytes
    grAttachment.query();
    var largeAttFiles = [];
    while (grAttachment.next()) {
        largeAttFiles.push(grAttachment.file_name + ' ('+(grAttachment.size_bytes/1048576).toFixed(2)+' MB)');
    }
    if (largeAttFiles.length > 0) {
        current.comments = 'The following attachments uploaded are larger than 3MB:\n' + largeAttFiles.join('\n');
		current.setWorkflow(false);
        current.update();
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I like this approach, especially the query-based check. I’m not sure if @renukakandu wants to enforce this for all incidents or only those created from the record producer. Technically, you should include a non-breaking space between the number and the unit, so like '\u00A0MB'.