Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Creating an incident from a record producer with an attachment, if attachment size is more than 3MB

Shantharao
Kilo Sage

Creating an incident from a record producer with an attachment, if attachment size is greater than 3 mb, need to populate the comments in the same incident script logic in servicenow

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

@Shantharao 

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

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.work_notes = '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

@Shantharao 

Hope you are doing good.

Did my reply answer your question?

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

Hi @Ankur Bawiskar ,
it is not working and client wanted the error message in the Portal,
When we added greater than 3mb attachment, need to show error message in the portal and stop the form submission 
Thanks in advance for you response, have a nice day

@Shantharao 

then you can try to use DOM manipulation and extract the file size and throw error

I don't think you can validate the file size during catalog item submission.

1) search anchor tag with this class "get-attachment ng-binding ng-scope"

2) iterate and extract the file name which includes the size

3) do string manipulation and grab the size in either KB, Bytes or MB

4) then add and validate

Example: I added 3 files so it will have 3 anchor tag with that class, you can extract the text within that anchor tag and do the next steps

AnkurBawiskar_0-1759733715579.png

 

 

AnkurBawiskar_1-1759733715606.png

 

 

AnkurBawiskar_2-1759733715604.png

 

 

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

Hi @Ankur Bawiskar 
Thank you for the response the attached screenshots are not clear, could you please provide the OnSubmit client script through validate the functionality.

Thanks in advance for your support and have a nice day