- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Hello @ManishS14752344,
- Open your Catalog Item.
- Go to the Variables related list and open your Attachment variable.
- Navigate to the Type Specifications tab.
- In the Variable attributes field, enter: allowed_extensions=pdf;jpg.
- Click Update.
function onSubmit() {
// List of allowed extensions
var validExtensions = ['pdf', 'jpg', 'jpeg'];
// Portal-specific attachment check
try {
var attachments = this.document.getElementsByClassName('get-attachment');
for (var i = 0; i < attachments.length; i++) {
var fileName = attachments[i].innerHTML.toLowerCase();
var fileExt = fileName.split('.').pop().split(' ')[0]; // Extract extension
if (validExtensions.indexOf(fileExt) === -1) {
alert('Invalid file type: ' + fileName + '. Only PDF and JPG are allowed.');
return false; // Stop submission
}
}
} catch (e) {
// Native UI fallback (optional)
var count = getSCAttachmentCount();
if (count > 0) {
// Further GlideAjax logic needed for Native UI deep validation
}
}
}Thanks
SP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Is it going to work for the OOTB attachment option? The one that says "Add Attachment" at the bottom of the form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
