In servicenow At table level I have file attachment field. This field should be visible and mandatory based on other field value. I am making this field as mandatory and visible via UI policy.
So till here its working fine, file attachment is visible and becoming mandatory. but issue is, even after attaching attachment also, still it remain as mandatory and not allowing to submit the record.
So, can anyone help me on this please.
Not looking for the attachment icon solution here, because I have multipl
Assistant
In ServiceNow, managing the visibility and mandatory status of a file attachment field through UI policies can be tricky because file attachment fields behave differently compared to standard fields. Despite making the attachment field mandatory via a UI policy, it might still flag as mandatory even after an attachment is uploaded.
To address this, you can use client scripts in conjunction with UI policies to dynamically check if an attachment has been added. Here is a step-by-step guide on how to achieve this:
Creating the UI Policy:
Create a new UI Policy that sets the file attachment field to visible and mandatory based on the value of another field.
Ensure your conditions are correctly set up to trigger this UI policy.
Using a Client Script:
Create a new Client Script to check if an attachment has been added and handle the mandatory state of the attachment dynamically.
Below is a sample onSubmit Client Script that you can use. This script checks if there are any attachments in the record and cancels the form submission if no attachment is found:
javascript
(function executeRule(current, previous /*null when async*/) {
var attachmentField = 'u_attachment_field'; // Replace with your attachment field name
var dependentField = 'dependent_field_name'; // Replace with the name of the dependent field
if (current.getValue(dependentField) == 'some_value') { // Replace 'some_value' with the condition value
var attachmentRecord = new GlideRecord('sys_attachment');
attachmentRecord.addQuery('table_sys_id', current.sys_id);
attachmentRecord.query();
if (!attachmentRecord.next()) {
current.addErrorMessage('Attachment is mandatory.');
gs.addErrorMessage('Attachment is mandatory.');
return false;
}
}
return true;
})(current, previous);
Incorporating the Check Into the Client Script:
Create a client script that runs onLoad and sets the attachment field to mandatory or not based on the condition. You can also use this script to dynamically check attachments and enforce validation.
Example of an onSubmit client script:
javascript
function onSubmit() {
var dependentField = g_form.getValue('dependent_field_name'); // Replace with the dependent field name
var isMandatory = false;
if (dependentField == 'some_value') { // Replace 'some_value' with the condition value
var ga = new GlideAjax('AttachmentChecker'); // Script Include name
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function (response) {
if (response === 'true') {
isMandatory = true;
} else {
g_form.addErrorMessage('Attachment is mandatory.');
g_form.submitted = false;
}
});
}
return true;
}
Creating the Server-Side Script Include:
Create a Script Include to check for attachments and return the result to the client script.
javascript
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var sysID = this.getParameter('sys_id');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sysID);
gr.query();
return gr.hasNext(); // Returns true if there are attachments, false otherwise
}
});
In the client script, getXMLAnswer would asynchronously call the script include to check for attachments, and if none are found, it prevents the form from submitting.
By using both UI policies and client scripts together, you can effectively manage the mandatory state and validation of attachment fields based on other field values.