Need help on business rule

Nivedita9
Tera Contributor

Hi,
I had a requirement to restrict attachment upto 25 mb on few tables. This functionality works fine it does restrict user from uploading file more than 25 MB but it temporary add empty file (adding as screenshot) which disappears when I reload the form.
Business Rule:

  try {
        var checkAttachmentSize;
        var attachmentFlag = false;
        // List of tables to check
        var allowedTables = ['task', 'cmdb_ci'];
        var attachemtTable = current.table_name;
        // Get the base table for the given table name
        var baseTable = String(new global.Hnk_project_process().getBaseTable(attachemtTable));
        var grRecord = new global.Hnk_project_process().getScope(attachemtTable);
        var kbRecord = new global.Hnk_project_process().getKnowledgeBase(current.table_sys_id);
        // If the base table matches and the attachment exceeds max size, restrict it
        // gs.log("Knowledge Base--" + kbRecord + 'and' + attachemtTable, '144');
        if ((allowedTables.includes(baseTable) && !grRecord.startsWith('Human')) || (attachemtTable == 'kb_knowledge' && !kbRecord.startsWith('HR'))) {
            checkAttachmentSize = new global.Hnk_project_process().getAttachmentSize(current);
            var totalSize = parseInt(current.size_bytes) + checkAttachmentSize;
            var maxSize = 25 * 1024 * 1024;
            if (totalSize > maxSize) {
                attachmentFlag = true;
                gs.addErrorMessage('Total attachments on this record cannot exceed 25 MB.');
                current.setAbortAction(true);
            }
        }
    } catch (ex) {
        gs.error("Attachment could not be find." + ex.message);
    }

Nivedita9_0-1747129883191.png

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Nivedita9 

your business rule should be on sys_attachment table

Seems you have some script include function getting called from "Hnk_project_process"

Did you debug that?

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

@Ankur Bawiskar 
Yes I'm getting proper return from my script include. This business rule is written on attachment table ,before and insert is condition.
Script Include is generic and client callable. Below are my functions :

  getAttachmentSize: function(attachRec) {
        var totalSize = 0;
        // Maximum size in bytes
        var attachmentFlag = false;
        var attachmentGr = new GlideRecord('sys_attachment');
        attachmentGr.addQuery('table_name', attachRec.table_name);
        attachmentGr.addQuery('table_sys_id', attachRec.table_sys_id);
        attachmentGr.query();
        while (attachmentGr.next()) {
            totalSize += parseInt(attachmentGr.size_bytes, 10);
        }
        return totalSize;
    },
    /**
     * Retrieves the scope for the given table name.
     * @function getScope
     * @param (string) tableName - The name of the table for which scope needs to be identified
     * @returns (string) = The scope of the table.
     * super_class=310415926f312200db45c0354b3ee44e^ORsuper_class=786359526f312200db45c0354b3ee480
     */
    getScope: function(tableName) {
        var tableScope;
        var taskID = String(gs.getProperty('hnk.base.table.ID'));
        var cmdbCI = String(gs.getProperty('hnk.cmdb.ci.table.ID'));
        var grScope = new GlideRecord('sys_db_object');
        grScope.addEncodedQuery('super_class.sys_id=' + taskID + '^ORsuper_class.sys_id=' + cmdbCI + '^name=' + tableName);
        grScope.query();
        if (grScope.next()) {
            tableScope = grScope.sys_scope.getDisplayValue();
        }
        return tableScope;
    },

    getKnowledgeBase: function(tableName) {
        var articleKB;
        var kbRec = new GlideRecord('kb_knowledge');
        kbRec.addEncodedQuery('sys_id='+ tableName);
        kbRec.query();
        if (kbRec.next()) {
            articleKB = kbRec.getDisplayValue('kb_knowledge_base');
        }
        return articleKB;
    },
    /**Retrives the base table for the given table name.
     * @function getBaseTable
     * @param (string) tableName - The name of the table for which base table needs to be identified
     * @returns (string) = The absolute base table name.
     */
    getBaseTable: function(tableName) {
        var tableUtils = new TableUtils(tableName);
        return tableUtils.getAbsoluteBase();
    },

@Nivedita9 

so is it creating a blank file everytime and still showing error message all the time?

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

yes and that file is not saved anywhere I believe when I reload the form that empty file disappears