The CreatorCon Call for Content is officially open! Get started here.

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

G Ponsekar
Giga Guru

Hi @Shantharao ,

 

Can you try with insert Business rule in Attachment table

Condition : Table name is incident

 

Sample Script:

    // Set the attachment size limit to 3 MB in bytes
    var ATTACHMENT_SIZE_LIMIT = 3 * 1024 * 1024;
    
    // Check if the current attachment's size is greater than the limit
    if (current.size_bytes > ATTACHMENT_SIZE_LIMIT) {
        
        // Use GlideRecord to find and update the parent incident record
        var incidentGr = new GlideRecord('incident');
        if (incidentGr.get(current.table_sys_id)) {
            incidentGr.comments = 'One or more attachments on this incident are larger than 3 MB. Please review them as necessary.';
            incidentGr.update();
        }
    }

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Rafael Batistot
Kilo Patron

Hi @Shantharao 

 

If you am want to set a limit for all intance: 

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0718101

 

if you want some specific for you case in incident, may you need create some customized 

 

See an example:

 

https://www.servicenow.com/community/csm-forum/how-to-define-maximum-attachment-size-for-one-table/m...

 

You don’t need apply the logic in record producer directly

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Hi @Rafael Batistot 
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

Hi @Shantharao ,

 

Can you check this article and adjust code as per your requirement: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0723548#:~:text=Views%20Engli...

 

Have client callable script include:

var AttachmentSizeChecker = Class.create();
AttachmentSizeChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkAttachmentSize: function() {
        var maxSizeBytes = this.getParameter('sysparm_max_size_bytes');
        var recordSysId = this.getParameter('sysparm_record_sys_id');

        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_sys_id', recordSysId);
        grAttachment.query();

        while (grAttachment.next()) {
            if (grAttachment.size_bytes > maxSizeBytes) {
                return 'true'; // Attachment size exceeded            }
        }
        return 'false'; // No attachments exceeded the size limit    },

    type: 'AttachmentSizeChecker'});

 

And onSubmit Client script:

function onSubmit() {
    

    var recordSysId = g_form.getUniqueValue();
    // Set your maximum file size in bytes (3 MB = 3 * 1024 * 1024)
    var maxSizeBytes = 3145728;

    var ga = new GlideAjax('AttachmentSizeChecker');
    ga.addParam('sysparm_name', 'checkAttachmentSize');
    ga.addParam('sysparm_record_sys_id', recordSysId);
    ga.addParam('sysparm_max_size_bytes', maxSizeBytes);
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            // Display an error message in the portal            alert('One or more attachments exceed the 3MB size limit.');
                       return false;        } else {
                        return true;        }
    });

   
    return false;
}

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP