How to make attachment Mandatory for Approval in sysapproval_approver

armanoj
Mega Sage

Hi,

 

How to make attachment mandatory approval table .

9 REPLIES 9

vaishali231
Kilo Sage

Hey @armanoj 

You can enforce this using a Before Update Business Rule on the sysapproval_approver table. Before allowing the approval, check whether an attachment exists for the current approval record in the sys_attachment table. If no attachment is found, abort the update and display an error message.

(function executeRule(current, previous) {
   if (current.state.changesTo('approved')) {
       var att = new GlideRecord('sys_attachment');
       att.addQuery('table_name', current.getTableName());
       att.addQuery('table_sys_id', current.sys_id);
       att.query();
       if (!att.hasNext()) {
           gs.addErrorMessage('Please attach a document before approving the record.');
           current.setAbortAction(true);
       }
   }
})(current, previous);

If you also want to make attachments mandatory when the approval is Rejected, include:

current.state.changesTo('approved') || current.state.changesTo('rejected')

in the condition.

This server-side approach is recommended since it works regardless of whether the approval is updated from the UI, Flow Designer, or an API.

 

***********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb




in this I'm getting another error also "Invalid value on Update"

Hey @armanoj 

The "Invalid value on Update" message is likely not caused by the attachment validation itself. current.setAbortAction(true) stops the update, but if the approval state is being changed through the OOB Approve/Reject UI Action or another Business Rule/Data Policy, the platform may also throw this generic error.

To troubleshoot:

  1. Temporarily disable the attachment validation and verify whether the approval updates successfully.
  2. Check for any other Business Rules, Data Policies, ACLs, or UI Actions on sysapproval_approver that might also be aborting the transaction.
  3. Review the system logs (System Logs > All) for the actual server-side exception.

If the only goal is to require an attachment before approval, the following validation is sufficient:

var att = new GlideRecord('sys_attachment');

att.addQuery('table_name', current.getTableName());

att.addQuery('table_sys_id', current.sys_id);

att.query();

if (!att.hasNext()) {

   gs.addErrorMessage('Please attach a document before approving the record.');

   current.setAbortAction(true);

}

If you're seeing both "Please attach a document..." and "Invalid value on Update", then another validation is almost certainly running in parallel. Can you confirm whether you're approving the record from the Approval form, Related List, Workspace, or Email Approval? That will help identify the source of the second error.

 

********************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb






Tanushree Maiti
Tera Patron

Hi @armanoj 

 

Try this:

1: Create the Script Include

  • Navigate to System Definition > Script Includes >Click New
    • Name: checkApprovalAttachment
    • Client callable: Check the box (true)

 

var chkApprovalAttachment = Class.create();

chkApprovalAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    hasAttachment: function() {

        var approvalId = this.getParameter('sysparm_approval_id');

        var grAttach = new GlideRecord('sys_attachment');

        grAttach.addQuery('table_sys_id', approvalId);

        grAttach.query();

 

        if (grAttach.next())

        {

            return 'true';

        }

        return 'false';

    },

    type: 'chkApprovalAttachment'

});

 

 

2: Create the Client Script

Navigate to System Definition > Client Scripts>Click New

    • Name: Mandatory Attachment on Approval
    • Table: Approval [sysapproval_approver]
    • Type: onSubmit
    • UI Type: All

 

function onSubmit() {

        var state = g_form.getValue('state');

    if (state == 'rejected') {

        return true;

    }

    var ga = new GlideAjax('chkApprovalAttachment');

    ga.addParam('sysparm_name', 'hasAttachment');

    ga.addParam('sysparm_approval_id', g_form.getUniqueValue());

    var response = ga.getXMLWait();

    var result = response.documentElement.getAttribute("answer");

    if (result == 'false') {

        g_form.addErrorMessage('An attachment is required before you can submit your approval.');

        return false;

    }

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti