- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 11:46 PM
Hello SNC,
I want to allow only xl sheet format as attachment for a catalog item.
I wrote the below script but it seems not working.
Any advice?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var attachment_sys_id = g_form.getValue('attachment');
var gsysa = new GlideRecord('sys_attachment');
gsysa.addQuery('sys_id', attachment_sys_id);
gsysa.query();
while (gsysa.query()) {
if (grsa.getValue('file_name').indexOf('.xlsx') != -1 || grsa.getValue('file_name').indexOf('.xls') != -1) {
return true;
} else {
g_form.addErrorMessage("Only XL sheet can be uploaded for this catalog item");
return false;
}
}
}
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 02:03 AM
Hello Kingstan,
Could you please check if this helps you to complete your requirement:
onSubmit Client script:
function onSubmit() {
var recordGuid = g_form.getUniqueValue();;
var ga = new GlideAjax('CustomAttachmentUtils');
ga.addParam('sysparm_name', 'validateAttachment');
ga.addParam('sysparm_record_guid', recordGuid);
ga.getXMLWait();
var isAttachmentValid = ga.getAnswer();
if (isAttachmentValid == "false") {
alert('Only files with xls are allowed');
return false;
}
}
Create a System property:
Name: allowed.file.extension
Value: xls,xlsx
Note in this property you can put the allow file extension separated by comma in case of more than 1 extension for example xls,csv,xlsx
Script Include:
var CustomAttachmentUtils = Class.create();
CustomAttachmentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateAttachment: function() {
var isValid = true;
var recordGuid = this.getParameter('sysparm_record_guid');
var sysAttachment = new GlideRecord('sys_attachment');
sysAttachment.addQuery('table_sys_id', recordGuid);
sysAttachment.query();
while (sysAttachment.next()) {
var fileName = sysAttachment.getValue("file_name");
var splitFileName = fileName.split(".");
var fileExtension = splitFileName[splitFileName.length - 1];
if (gs.getProperty("allow.file.extension").indexOf(fileExtension) == -1) {
isValid = false;
break;
}
}
return isValid;
}
type: 'CustomAttachmentUtils'
});
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 11:48 PM
Hi
Check out this thread that may help you:
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 11:55 PM
In fact i referred the same but no help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 12:23 AM
Hi
it seems declaration of variable shows different in below code
grsa--> gsysa
if (gsysa .getValue('file_name').indexOf('.xlsx') != -1 || gsysa .getValue('file_name').indexOf('.xls') != -1) { return true;
also add code in on submit instead of on change, try to keep alert and check is there any other issues in code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 02:03 AM
Hello Kingstan,
Could you please check if this helps you to complete your requirement:
onSubmit Client script:
function onSubmit() {
var recordGuid = g_form.getUniqueValue();;
var ga = new GlideAjax('CustomAttachmentUtils');
ga.addParam('sysparm_name', 'validateAttachment');
ga.addParam('sysparm_record_guid', recordGuid);
ga.getXMLWait();
var isAttachmentValid = ga.getAnswer();
if (isAttachmentValid == "false") {
alert('Only files with xls are allowed');
return false;
}
}
Create a System property:
Name: allowed.file.extension
Value: xls,xlsx
Note in this property you can put the allow file extension separated by comma in case of more than 1 extension for example xls,csv,xlsx
Script Include:
var CustomAttachmentUtils = Class.create();
CustomAttachmentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateAttachment: function() {
var isValid = true;
var recordGuid = this.getParameter('sysparm_record_guid');
var sysAttachment = new GlideRecord('sys_attachment');
sysAttachment.addQuery('table_sys_id', recordGuid);
sysAttachment.query();
while (sysAttachment.next()) {
var fileName = sysAttachment.getValue("file_name");
var splitFileName = fileName.split(".");
var fileExtension = splitFileName[splitFileName.length - 1];
if (gs.getProperty("allow.file.extension").indexOf(fileExtension) == -1) {
isValid = false;
break;
}
}
return isValid;
}
type: 'CustomAttachmentUtils'
});
Please mark my respsone as helpful/correct, if it answer your question.
Thanks