Help Needed: Preventing Attachment of .zip Files in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 12:12 AM
Hello ServiceNow Community,
I hope this message finds you well. I'm currently facing an issue with preventing the attachment of .zip files in my ServiceNow instance. Despite implementing a script to restrict the attachment of .zip files, null entries still appear in the Manage Attachments section after an attempt to attach a .zip file.
Here's a summary of the problem and what I've done so far:
When attempting to attach a .zip file, although I receive the expected error message indicating that .zip files are not allowed, null entries still appear in the Manage Attachments section.
What I've Tried: I've implemented a business rule script which runs on the sys_attachment table and specifically targets the before insert event. In the advanced condition section of the business rule, I've specified the condition current.table_name == 'u_random_table' to ensure that the script only executes when attachments are being added to records in the u_random_table. Despite these efforts, it seems that the script is not entirely successful in preventing the creation of attachment records for .zip files, leading to the appearance of null entries in the Manage Attachments section.
Script Details:
(function executeRule(current, previous /*null when async*/) {
if (current.getValue('random_table_name') == 'u_random_table') {
var fileName = current.getValue('file_name');
// Check if the file name contains ".zip"
var zipIndex = fileName.indexOf('.zip');
// If ".zip" is found in the file name, display error message and abort
if (zipIndex != -1) {
gs.addErrorMessage('Attachment of .zip files is not allowed.');
current.setAbortAction(true);
}
}
})(current, previous);
```
Request for Assistance:
I'm seeking guidance and insights from the community on how to properly prevent the attachment of .zip files without resulting in null entries in the Manage Attachments section. Any suggestions, alternative approaches, or insights into what might be causing this issue would be greatly appreciated. Please go through pic attached for your reference.
Thank you in advance for your help and support!
Thanks & regards,
Sunayana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 05:06 AM
Something like this:
Set you business rule with condition 'table_name = 'u_random_table'' on the attachment table, otherwise it runs on every attachment added to the system.
(function executeRule(current, previous /*null when async*/) {
// Get the file name of the attachment
var fileName = current.file_name.toString();
// Check if the file extension is .zip
if (fileName.endsWith('.zip')) {
// Add a message to inform the user
gs.addErrorMessage('Attachment of .zip files is not allowed.');
// Delete the attachment
current.deleteRecord();
// Optionally, abort the current action
gs.setAbortAction(true);
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 07:13 PM
Hi @Mark Manders and Community,
Thank you, Mark, for your suggestion. I set up the business rule as advised, but unfortunately, I'm still facing the same issue where .zip files appear as null entries in the Manage Attachments section.
Has anyone else encountered and resolved this issue? I need to prevent .zip files from being attached to records in a specific table and ensure no null entries appear.
Any further guidance or suggestions would be greatly appreciated.
Thank you for your help!
Thanks & Regards,
Sunayana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 08:25 PM
Hi @Sunayana4 ,
Script Include to check for Zip type attachments:
checkAttachmentType: function() {
var gr = new GlideRecord("sys_attachment");
gr.addEncodedQuery("content_type=application/zip^table_name=incident^table_sys_id=" + this.getParameter('sysparm_id'));
gr.query();
if (gr.next()) {
return false;
} else {
return true;
}
},
On Submit Client script on your table (here I am restricting zip files on incident table):
function onSubmit() {
var ga = new GlideAjax('YourScriptIncludeName');
ga.addParam('sysparm_name', 'YourScriptIncludeFunctionName');
ga.addParam('sysparm_id',g_form.getUniqueValue());
ga.getXMLAnswer(HelloWorldParse);
return false;
}
function HelloWorldParse(response) {
if (response === "false") {
g_form.addErrorMessage("ZIP files not allowed");
return false;
} else {
return true;
}
}
This will abort submission of record if ZIP file is attached to it. Tested in PDI.
I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.