How to check attachment before on submit client script in catalog item & attachment must be xlsx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
Kindly help me
How to check attachment before on submit client script in catalog item &
there must be 1 attachment and that attachment type must be xlsx only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This won't work in Service Portal, and the Isolate script box must be unchecked. These scripts are dynamic so they can be used in different scenarios - attached to various tables, types, file names, limits etc.
function onSubmit() {
var itemsysid = g_form.getValue('sysparm_item_guid');
if(!itemsysid){
itemsysid = gel('sysparm_item_guid').value;
}
var ga = new GlideAjax('CheckAttachment');
ga.addParam('sysparm_name', 'isAttached');
ga.addParam('sysparm_sysid', itemsysid);
ga.addParam('sysparm_tablename', 'sc_cart_item');
ga.addParam('sysparm_fileoperator', 'ENDSWITH');
ga.addParam('sysparm_filepattern', '.xlsx');
ga.getXMLWait();
var answer = ga.getAnswer();
answer = answer.evalJSON(); //Transform the JSON string to an object
if (answer.attached == 'false') {
alert("You must attach the Excel file.");
return false;
}
if (answer.count > 1) {
alert("Only one file can be attached.");
return false;
}
}var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isAttached: function() {
var obj = {};
obj.attached = 'false';
obj.count = 0;
var id = this.getParameter('sysparm_sysid');
var tablename = this.getParameter('sysparm_tablename'); //'sc_cart_item'
var fileoperator = this.getParameter('sysparm_fileoperator'); //'ENDSWITH'
var filepattern = this.getParameter('sysparm_filepattern'); //'.xlsx'
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', tablename);
if(filepattern){
attachment.addQuery('file_name', fileoperator, filepattern);
}
attachment.addQuery('table_sys_id',id);
attachment.query();
while (attachment.next()) {
obj.count++;
obj.attached = 'true';
}
var json = new JSON();
var data = json.encode(obj);//JSON formatted string
return data;
},
type: 'CheckAttachment'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Brad Bowman ,
Thanks for your reply,
can we use getXMLAnswer() rather than using getXMLWait().
ga.getXMLWait();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
above code working for native UI , but not working for Portal
function onSubmit() {
var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;
var ga = new GlideAjax('CheckAttachment');
ga.addParam('sysparm_name', 'isAttached');
ga.addParam('sysparm_sysid', itemSysId);
ga.addParam('sysparm_tablename', 'sc_cart_item');
ga.getXMLWait();
var result = JSON.parse(ga.getAnswer());
if (result.attached === 'false') {
alert("Attach exactly one file (xlsx, xls, pdf).");
return false;
}
if (result.count > 1) {
alert("Only one attachment is allowed.");
return false;
}
return true;
}var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isAttached: function() {
var obj = {
attached: 'false',
count: 0
};
var id = this.getParameter('sysparm_sysid');
var tablename = this.getParameter('sysparm_tablename');
var allowedTypes = ['xlsx', 'xls'];
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', tablename);
gr.addQuery('table_sys_id', id);
gr.query();
while (gr.next()) {
var name = gr.file_name.toString().toLowerCase();
var ext = name.split('.').pop();
if (allowedTypes.indexOf(ext) > -1) {
obj.count++;
obj.attached = 'true';
}
}
return JSON.stringify(obj);
},
type: 'CheckAttachment'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
The attachment variable does pretty much all you need and it is UI-agnostic. However if you can't use that for some reason you can achieve this in a few other ways as well.
The state for attachments is stored in the scope for the catalog item widget so you can just look it up from there. Or you can get the sysid that is generated for the cart item to do what @Brad Bowman 's script does. Keep in mind the asynchronicity of glideajax in onsubmits.
function onSubmit() {
var attachments = this.angular.element("#sc_cat_item").scope().attachments;
if (typeof attachments === "undefined" || attachments.length !== 1 || attachments[0].ext !== "xlsx") {
return false;
}
}
...
var atchTable = this.angular.element("#sc_cat_item").scope().data._attachmentTable;
var atchSysid = this.angular.element("#sc_cat_item").scope().data._generatedItemGUID;

