How to make attachment Mandatory for Approval in sysapproval_approver

armanoj
Mega Sage

Hi,

 

How to make attachment mandatory approval table .

9 REPLIES 9

@Tanushree Maiti ,

When I upload an attachment and try to submit the approval, an error is displayed. This happens because the attachment has not yet been saved to the sys_attachment table at the time the validation runs. As a result, the attachment check fails and the error message continues to appear, even though the user has already selected an attachment. The validation is occurring before the attachment record is fully created in the attachment table.

Hi @armanoj 

 

In that Could you please try with Before Insert/update BR:

 

 

  • Name: Validate Approval Attachment
  • Table: Approval [sysapproval_approver]
  • Active: Checked
  • When: Before
  • Insert , Update: Checked
  • Condition: State changes to Approved

 

 

(function executeRule(current, previous) {

if (current.state == 'approved') {
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', 'sysapproval_approver');
grAttach.addQuery('table_sys_id', current.sys_id);
grAttach.query();

if (!grAttach.hasNext()) {
gs.addErrorMessage('You must attach a document before approving this request.');
current.setAbortAction(true);
}
}
})(current, previous);

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

in this method another error message along with custom error message . " "Invalid value on Update"

Hello @armanoj ,

 

Yes it's bcz a timing problem when a user attaches a file on the Approval form, ServiceNow stages the attachment in browser but doesn't commit it to sys_attachment until after the form submits.

 

so for that try updating the client script using this :

var state = g_form.getValue('state');
    if (state == 'rejected') {
        return true;
    }

    if (g_form.hasAttachments()) {
        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 approve this record.');
        return false;
    }

    return true;

 

If my response helped mark as helpful and accept the solution.

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