
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2020 02:42 AM
Hello,
I want to check if attachments have been added to a catalog item.
The UI Script looks like this:
function getAttachments() {
var length;
var attachments;
try {
attachments = angular.element("#sc_cat_item").scope().attachments;
length = attachments.length;
}
catch(ex) {
//length = -1;
length = ex;
}
return length;
}
The Client Script looks like this:
function onSubmit() {
//Works in non-portal ui
try {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach a document before submitting this request.');
return false;
}
}
//For Service Portal
catch(e) {
var count = getAttachments();
alert(count);
if(count <= 0) {
alert('You must attach a document before submitting this request. ' + count);
return false;
}
}
}
The Alert (for debugging purposes) gives back the exception 'Cannot read property 'attachments' of undefined'
Isolate script is off:
I tried below solution as well (which I got from researching the possibilities):
function onSubmit() {
//Type appropriate comment here, and begin script below
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach document before submitting the request.');
return false;
}
}
But it gives me the error: 'TypeError: Cannot read property 'getElementById' of null'
Does anyone have suggestions?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2020 02:47 AM
Hi,
this should work fine
Ensure Isolate Script field is set to false for the onSubmit client script
This field is not on form but from list you can make it false
function onSubmit() {
//Type appropriate comment here, and begin script below
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must attach document before submitting the request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length == 0){
alert('You must attach document before submitting the request.');
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-19-2020 02:47 AM
Hi,
this should work fine
Ensure Isolate Script field is set to false for the onSubmit client script
This field is not on form but from list you can make it false
function onSubmit() {
//Type appropriate comment here, and begin script below
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must attach document before submitting the request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length == 0){
alert('You must attach document before submitting the request.');
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-19-2020 11:54 PM
It doesnt answer whats wrong with my script, but your solution works!
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2020 12:59 AM
You are welcome
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader