How to make attachment Mandatory for Approval in sysapproval_approver

armanoj
Mega Sage

Hi,

 

How to make attachment mandatory approval table .

14 REPLIES 14

in approver table attachment is not a field . So, code will search in sys_attachment table . When add a new attach in approval and try to submit the record this code will deny to submit.

Ankur Bawiskar
Tera Patron

@armanoj 

why attachment is mandatory for approval?

What's your exact business requirement here?

Approvers only approve/reject request by looking into the RITM

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

as per the requirement this TPRM module table , approver needs to add attachment and additional comments while approve or reject 

Hi @armanoj ,

I tested the script in my PDI and it worked as expected. In this scenario, you can use setWorkflow(false) to prevent the execution of other scripts while validating attachments. make sure it is not affecting other functionalities.

Set the source table to the TPRM module table name to ensure attachments are enforced only for the required tables.

Condition:
State changes to - Approved or Rejected AND
source table is "specify your TPRM table name"

NatrajS93604625_0-1781838930473.png


Before (insert/update) Business rule:

var sysattachGR = new GlideRecord('sys_attachment');
    sysattachGR.addQuery('table_name', 'sysapproval_approver');
    sysattachGR.addQuery('table_sys_id', current.sys_id);
    sysattachGR.setLimit(1);
    sysattachGR.query();
    if (!sysattachGR.next()) {
        gs.addErrorMessage('Attachment is Mandatory');
        //sysattachGR.setWorkflow(false);
        current.setAbortAction(true);
    }


If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.
Thanks,
Natraj Sankar

Tejas Adhalrao
Kilo Sage

Hi @armanoj  ,

Create a Before Update Business Rule on sysapproval_approver.

 

  • Table: sysapproval_approver
  • When: Before
  • Update: Checked
  • Advanced: Checked
  • conditon 

TejasAdhalrao_0-1781835417090.png

and script 

(function executeRule(current, previous) {



        // Check comments
        var comments = current.comments.getJournalEntry(1);
        if (gs.nil(comments)) {
            gs.addErrorMessage('Comments are mandatory while approving or rejecting.');
            current.setAbortAction(true);
            return;
        }

        // Check attachments
        var attGR = new GlideRecord('sys_attachment');
        attGR.addQuery('table_name', 'sysapproval_approver');
        attGR.addQuery('table_sys_id', current.sys_id);
        attGR.setLimit(1);
        attGR.query();

        if (!attGR.hasNext()) {
            gs.addErrorMessage('Attachment is mandatory while approving or rejecting.');
            current.setAbortAction(true);
            return;
        }
    

})(current, previous);

 

 

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

thanks ,

tejas 😊