Conditional Mandatory Attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2020 01:25 PM
I'm trying to require an attachment based on a condition. I have done this in the past for other projects but it's simply not working this time. I have tried a few answers I found from the Community but absolutely nothing is working. I am in the Human Resources: Service Portal application. I have a record producer that I've put an onSubmit client script on. I put alerts in to validate that my if statement is evaluating to true and it is, and my alert in the try statement is also firing. Here's my client script:
function onSubmit() {
// If employee's country is Ireland or United Kingdom and Reason for Change is Salary & Payments require an attachment
var reason = g_form.getValue('u_tcReason');
var country = g_form.getValue('user_country');
if (reason == 'Salary & Payments' && country == 'GB' || country == 'IE') {
alert('in the if statement for my condition. Country is ' + country);
try {
alert('in the try');
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert('Need attachments.');
return false;
}
} catch (e) {
var count = getSCAttachmentCount();
if (count <= 0) {
alert('Need attachments.');
return false;
}
}
}
}
Isolate script: false
UI Type: All
I've also tried this approach:
function onSubmit(){
var value = g_form.getValue('request_type');
if(value == 'b' || value == 'd'){
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must add an attachment before submitting this request.');
return false;
}
}
}
That doesn't work either. With both approaches I get this error message at the top of the form in native:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2020 01:53 PM
Hi,
Did you try this:
https://community.servicenow.com/community?id=community_question&sys_id=de340b29dbd8dbc01dcaf3231f9619a3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2020 02:05 PM
Hi Akshay,
Yes, I have tried that. That UI script does exist in the instance, but I wonder if it's not working because it's in Global on sc_req_item. I'm in an HR scope and on the sn_hr_core_case table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2020 07:52 AM
I recently worked something similar for HR Core scope and it worked well for me in Scoped app for record producer
please use this script and try once
function onSubmit() {
// If employee's country is Ireland or United Kingdom and Reason for Change is Salary & Payments require an attachment
var reason = g_form.getValue('u_tcReason');
var country = g_form.getValue('user_country');
if (reason == 'Salary & Payments' && ( country == 'GB' || country == 'IE')) {
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('Need attachments.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length == 0){
alert('Need attachments.');
return false;
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2020 08:15 AM
There's no system property or anything that would be hindering the native portion of the code that I can check, right?