- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 03:19 AM - edited 03-31-2025 03:40 AM
Hi,
Am using below code to restrict file attachment type. The Portal and Non-Portal Code was working.
But as per ServiceNow suggestion it's not good to use DOM method. I did replacement with this line of code (this.g_form.getFormElementByClassName) for DOM in Portal script but it is not working.
So, please suggest alternatives to achieve my requirement.
Here is my code:-
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 04:56 AM
if you want to restrict file type then better go with attachment variable type and use allowed_extension attributes to only allow particular file type
something like this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 05:35 AM
Yes @Ankur Bawiskar Did it this before and it was working. Anyway thanks for your quick response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 03:28 AM
will suggest to try with following code which uses Attachment API.
if (g_form.getValue('request_type') === "new font request") {
var attachments = g_form.getAttachments();
if (!attachments || attachments.length === 0) {
alert("Attachment Required!!");
return false;
}
var validExtensions = ['woff', 'woff2'];
for (var i = 0; i < attachments.length; i++) {
var fileName = attachments[i].file_name.toLowerCase();
var fileExtension = fileName.split('.').pop();
if (!validExtensions.includes(fileExtension)) {
alert("Attachment should be in only woff/woff2 format!");
return false;
}
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 03:37 AM
Thank you for suggestion @Nilesh Pol used suggested script but it is allowing attachment instead of showing alert message attachment required.
My Requirement is need to restrict the attachment (if other then woff/woff2) in End user Portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 03:54 AM
@e__rajesh_badam Since g_form.getAttachments() does not work in the Service Portal, you need to use the Attachment Validation Script in the onSubmit client script to enforce restrictions.
use the updated script:
function onSubmit() {
if (g_form.getValue('request_type') === "new font request") {
// Get attachments using spAttachment API
var attachments = window.top.CustomEvent ? window.top.CustomEvent.attachments : [];
if (!attachments || attachments.length === 0) {
alert("Attachment Required!!");
return false;
}var validExtensions = ['woff', 'woff2'];
for (var i = 0; i < attachments.length; i++) {
var fileName = attachments[i].name.toLowerCase();
var fileExtension = fileName.split('.').pop();
if (!validExtensions.includes(fileExtension)) {
alert("Attachment should be in only .woff/.woff2 format!");
return false;
}
}
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 04:14 AM
Sorry @Nilesh Pol Still script is not restricting, request is getting submit even after if i wont attach files while submitting request.