How to make attachment Mandatory for Approval in sysapproval_approver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
How to make attachment mandatory approval table .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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;
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
in this method another error message along with custom error message . " "Invalid value on Update"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.