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
Hi @lauri457 ,
Syn working in Native UI, not working for Portal
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'
});
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can't get the sysid like this on the portal
var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;But it can be found from the catitem widgets scope
this.angular.element("#sc_cat_item").scope().data._generatedItemGUID;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
so then , it should be like below ?
if(window==null)
if(window==null)
var itemSysId = this.angular.element("#sc_cat_item").scope().data._generatedItemGUID;
else
var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Easiest way
-> create attachment variable and make it mandatory at variable config level (handles only 1 attachment check)
-> add this in variable attributes so that it allows only xlsx file
š” If my response helped, please mark it as correct ā and close the thread šā this helps future readers find the solution faster! š
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
If you don't want to use attachment variable then use this script in onSubmit catalog client script
function onSubmit() {
//Type appropriate comment here, and begin script below
var arr = [];
var hiddenVariable = g_form.getValue('hidden_variable');
try {
if (window == null) {
// portal
var z = this.document.getElementsByClassName("get-attachment ng-binding ng-scope");
var k;
for (k = 0; k < z.length; k++) {
var value = z[k].innerHTML;
value = value.substring(0, value.indexOf('('));
arr.push(value.trim());
}
// now check if the file name has extension as xlsx or not by iterating array
}
} catch (ex) {
// native get all the file names
$j("a.content_editable").each(function(index) {
var val = $j(this).text();
arr.push(val);
});
// now check if the file name has extension as xlsx or not by iterating array
}
}
š” If my response helped, please mark it as correct ā and close the thread šā this helps future readers find the solution faster! š
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

