- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 10:28 AM
I have a catalog item, when the user attaches any document while filling catalog form do not allow the form to submit, if the attached document are in these formats pdf, docx.
Anyone help me how to fetch attached document names, format and to achieve the above thing. I want to Perform this in Portal Side. If user attaches other than these documents show some error or alert message.
Solved! Go to Solution.
- 1,001 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 07:44 PM - edited 06-30-2024 10:15 PM
hi @Ramesh_143
you can achieve your functionality by writing an onSubmit() script for the form. Here is the script:
function onSubmit() {
var a = this.document.getElementsByClassName('get-attachment');
var b = a.length;
//alert(b);
if (!b) {
g_form.addErrorMessage('You must add an attachment');
return false;
} else {
for (var i = 0; i < a.length; i++) {
var t = a[i].title;
var fileExtension = t.split('.').pop().toLowerCase();
if (fileExtension != 'docx' && fileExtension != 'pdf') {
g_form.addErrorMessage(fileExtension + ' ' + 'You must add file types of docx and xlsx only');
return false; // Prevent form submission
}
}
return true;
}
}
- Here When I attached a document it will check weather it is in xlsx or docs or not. For Example, I am attaching Text document then it will give an error message.
- The Below Client side Script works when the form is viewed on the ServiceNow Portal. It involves Manipulating the DOM (Document Object Model) Events.
- Direct DOM access is not Possible on the ServiceNow Platform(Backend) using client-side scripts that run in users browser.
If my solution gets resolved your problem, Please give me a like and make my answer as 'correct answer'.
Thanks ,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 04:25 AM
Hi @Ramesh_143
To restrict document attachments in a ServiceNow catalog item to only specific formats (PDF and DOCX) on the portal side, you can use a client script to validate the file types before submission.
Thanks, and Regards
Vishaal
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 12:46 PM
You need an onSubmit client script that would make an Ajax call to a client-callable script include, which in its turn would query the attachments, analyze their names, and return the result of the check to the client script.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 08:11 PM
To restrict form submission in ServiceNow when attached documents are not in PDF or DOCX format:
-
Attach Client Script: Add a client script to the form's client scripts section.
-
Validation Logic: Use JavaScript to check attachments before submission.
function onSubmit() {
var attachmentField = g_form.getControl('attachment_field_name'); if (attachmentField) { var attachments = attachmentField.files; for (var i = 0; i < attachments.length; i++) { var fileName = attachments[i].name.toLowerCase(); if (!fileName.endsWith('.pdf') && !fileName.endsWith('.docx')) { alert('Attach only PDF or DOCX files.'); return false; } } } return true; } g_form.submit = onSubmit;
-
Implementation: Replace
'attachment_field_name'
with your actual attachment field name. -
Testing: Ensure attachments are validated correctly during testing.
This approach uses client-side JavaScript to enforce file format validation before allowing form submission in ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 07:44 PM - edited 06-30-2024 10:15 PM
hi @Ramesh_143
you can achieve your functionality by writing an onSubmit() script for the form. Here is the script:
function onSubmit() {
var a = this.document.getElementsByClassName('get-attachment');
var b = a.length;
//alert(b);
if (!b) {
g_form.addErrorMessage('You must add an attachment');
return false;
} else {
for (var i = 0; i < a.length; i++) {
var t = a[i].title;
var fileExtension = t.split('.').pop().toLowerCase();
if (fileExtension != 'docx' && fileExtension != 'pdf') {
g_form.addErrorMessage(fileExtension + ' ' + 'You must add file types of docx and xlsx only');
return false; // Prevent form submission
}
}
return true;
}
}
- Here When I attached a document it will check weather it is in xlsx or docs or not. For Example, I am attaching Text document then it will give an error message.
- The Below Client side Script works when the form is viewed on the ServiceNow Portal. It involves Manipulating the DOM (Document Object Model) Events.
- Direct DOM access is not Possible on the ServiceNow Platform(Backend) using client-side scripts that run in users browser.
If my solution gets resolved your problem, Please give me a like and make my answer as 'correct answer'.
Thanks ,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 04:25 AM
Hi @Ramesh_143
To restrict document attachments in a ServiceNow catalog item to only specific formats (PDF and DOCX) on the portal side, you can use a client script to validate the file types before submission.
Thanks, and Regards
Vishaal
Please mark this response as correct or helpful if it assisted you with your question.