How to get the attachment format/type after attaching the attachment on a catalog form before submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2024 09:36 PM
I have a Single Line Text Variable - File format/Type: and Attachment Variable. After attaching a file in the Attachment variable. It should display the file format in the above Single Line Text Variable : File format/Type:
Example: If I attach PDF file. It should display the as PDF on the above Single Line Text Variable - File format/Type:
Attachment should allow only PDF/TFF formats only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2024 09:55 PM
To retrieve the attachment format or type (such as the MIME type) of a file attached to a catalog form in ServiceNow before submission, you can use a Catalog Client Script. This script allows you to interact with the form and access attachment data before the form is submitted. The GlideAttachment API can be used to retrieve the MIME type of the attachment. Specifically, you can create a client script that runs when a user attaches a file, querying the sys_attachment table to fetch the details of the attachment linked to the catalog record. The MIME type of the attachment is stored in the content_type field in the sys_attachment table, which can be accessed to determine the format . By using this approach, you can capture and log the file type or perform validations based on the file format before the form is submitted. This allows you to enforce rules, such as restricting certain file types, or simply track the attachment details before final submission of the catalog request.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2024 09:55 PM
You can use UI Policy and Client Script on your Catalog item to validate the file format.
Client Script for Catalog Item:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var allowed_formats = ['pdf', 'tiff'];
var file_name = newValue.split('/').pop();
var file_type = file_name.split('.').pop().toLowerCase();
if (allowed_formats.includes(file_type)) {
g_form.setValue('<single_line_text_variable_name>', file_type.toUpperCase());
} else {
g_form.setValue('<single_line_text_variable_name>', '');
alert('Invalid file format. Only PDF/TFF files are allowed.');
}
}
UI Policy Script for Catalog Item:
g_form.getControl('<attachment_variable_name>').addEventListener('change', function (event) {
var allowed_formats = ['pdf', 'tiff'];
var files = event.target.files;
if (files.length > 0) {
var file_type = files[0].name.split('.').pop().toLowerCase();
if (!allowed_formats.includes(file_type)) {
alert('Only PDF/TFF files are allowed.');
event.target.value = '';
}
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2024 09:59 PM
To retrieve the attachment format or type (such as the MIME type) of a file attached to a catalog form in ServiceNow before submission, you can use a Catalog Client Script. This script allows you to interact with the form and access attachment data before the form is submitted. The GlideAttachment API can be used to retrieve the MIME type of the attachment. Specifically, you can create a client script that runs when a user attaches a file, querying the table to fetch the details of the attachment linked to the catalog record. The MIME type of the attachment is stored in the field in the table, which can be accessed to determine the format By using this approach, you can capture and log the file type or perform validations based on the file format before the form is submitted. This allows you to enforce rules, such as restricting certain file types, or simply track the attachment details before final submission of the catalog request.