Skip to content

Commit 7661a0a

Browse files
authored
Client Validation of Attachments by File Type and Count (#1970)
* Create code.js * Create README.md
1 parent 0b1046d commit 7661a0a

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

  • Client-Side Components/Client Scripts/Client Validation of Attachments by File Type and Count
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This ServiceNow client script is designed to validate file attachments on a form before submission. It's most likely used as a "Client Script" or "Client Script in a Catalog Item" that runs in the browser when a user tries to submit a form.
2+
3+
This client script runs when a form is submitted in ServiceNow. It checks if the user has:
4+
5+
Attached at least one file (shows an error if none).
6+
Attached no more than three files (shows an error if more).
7+
Only uploaded files of type PDF or PNG (shows an error for other types).
8+
If any of these checks fail, the form submission is blocked and an appropriate error message is displayed.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(function executeRule(gForm, gUser, gSNC) {
2+
var attachments = gForm.getAttachments();
3+
if (!attachments || attachments.length === 0) {
4+
gForm.addErrorMessage("You must attach at least one file.");
5+
return false;
6+
}
7+
8+
if (attachments.length > 3) {
9+
gForm.addErrorMessage("You can only upload up to 3 files.");
10+
return false;
11+
}
12+
13+
var allowedTypes = ['pdf', 'png'];
14+
for (var i = 0; i < attachments.length; i++) {
15+
var fileName = attachments[i].file_name.toLowerCase();
16+
var ext = fileName.split('.').pop();
17+
if (!allowedTypes.includes(ext)) {
18+
gForm.addErrorMessage("Only PDF and PNG files are allowed.");
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
})(gForm, gUser, gSNC);

0 commit comments

Comments
 (0)