UI Script returns "Error message: 'Cannot read property 'attachments' of undefined' " (Check for attachments)

Simon Ohle
Kilo Guru

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:

 

find_real_file.png

 

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?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

It doesnt answer whats wrong with my script, but your solution works!

 

Thank you!

You are welcome

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader