Validating Attachment extension based on type of Attachment in Catalog Request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
In My catalog item There is a variable Called "file_type" it is a select box type in that there is 3 options like txt,xlsx,csv. If file type is csv, then In Catalog item there is a variable called "Data Attachment" variable it is a attachment type. It will allow to user to attach only csv file. Like that remaing also If user selects txt type then it will allow to attach the txt file attachment only.
For this I have created a On change client script on data_attachment variable.
Client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @jreddy7 ,
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
g_form.hideFieldMsg('data_attachment');
var selectedFileType = g_form.getValue('file_type');
if (!selectedFileType) {
g_form.clearValue('data_attachment');
g_form.showFieldMsg(
'data_attachment',
'Please select a file type before uploading.',
'error'
);
return;
}
var ga = new GlideAjax('AttachmentValidator');
ga.addParam('sysparm_name', 'validateType');
ga.addParam('sysparm_attachment_id', newValue);
ga.addParam('sysparm_selected_type', selectedFileType);
ga.getXMLAnswer(function(answer) {
if (answer === 'invalid') {
g_form.showFieldMsg(
'data_attachment',
'Invalid file type. Please upload only .' +
selectedFileType + ' files.',
'error'
);
// Clear the attachment variable
g_form.clearValue('data_attachment');
}
});
}
script include
var AttachmentValidator = Class.create();
AttachmentValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateType: function() {
var attachmentSysId = this.getParameter('sysparm_attachment_id');
var selectedType = this.getParameter('sysparm_selected_type');
if (!attachmentSysId || !selectedType) {
return 'valid';
}
var gr = new GlideRecord('sys_attachment');
if (gr.get(attachmentSysId)) {
var fileName = gr.getValue('file_name').toLowerCase();
var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExtension !== selectedType.toLowerCase()) {
return 'invalid';
}
}
return 'valid';
},
type: 'AttachmentValidator'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @jreddy7
- Navigate to your Catalog Item and open the data_attachment variable.
- Switch to the Type Specifications tab.
- In the Variable attributes field, you have mentioned:
allowed_extensions=csv;xlsx;txt
mention all type file extensions here, which you want to allow at form level attachment variable.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti