Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Restricting add attachment in service portal to accept only PDF and JPG

ManishS14752344
Tera Contributor

Hi,

 

So I know there are numerous solution available so please refrain from quoting another post or pasting links.

 

I have a requirement where I have to make sure that only PDF and JPG files are attached on the 'Add Attachment' in my catalog item. 

 

It can be done using the following script

 

function onSubmit() {
   
	  //Check and see if there is atleast one attachment, if none, attachment is undefined
    if (this.angular.element("#sc_cat_item").scope().attachments !== undefined) {
		
		  //Get the array of objects, each attachment has it's one object with data in the array 
		  var attachArr = this.angular.element("#sc_cat_item").scope().attachments;
		
		  //If we find a PDF-file, let the user submit
		  if (attachArr.map(function (attach){return attach.ext}).indexOf('pdf') >= 0 ){
			  return true;
		  }
    }
	
	  //If there isn't any PDF attached, dont let them submit it.
		alert('You need to attach an PDF-file to be able to submit.')	
    return false;
}

 

This should work but the issue is, it is using DOM and ServiceNow does not promote using DOM in service portal. 

Is there a way to do this using script include?

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@ManishS14752344 

No solution without DOM is available

Only approach is to use Variable of type Attachment and then add attributes in that variable

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

5 REPLIES 5

I also have the same opinion @Ankur Bawiskar . I was just looking for an alternative just in case. 
Thank you for clarifying