Onsubmit client script not validating attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
I'm attempting to validate that a specific attachment is included on a cat_item before it can be submitted.
Script:
function onSubmit() {
var curUsr = g_user.userID;
var mgr = g_form.getValue('requested_manager');
if(curUsr!=mgr){
var ga = new GlideAjax('AttachmentUtils2');
ga.addParam('sysparm_name', 'hasRequiredAttachment');
ga.addParam('sysparm_sys_id', g_form.getValue('sysparm_item_guid'));
// tried using g_form.getUniqueValue()), but this didn't return the corred sys_id for the attachments record.
ga.addParam('sysparm_search', 'Manager Approval');
ga.getXMLWait();
var answer = ga.getAnswer();
if (answer !== 'true') {
g_form.addErrorMessage('You must attach a file containing "Manager Approval" in the file name before submitting or have them submit this form.');
return false; // Aborts form submission
}
}
Script Include
var recordId = this.getParameter('sysparm_sys_id');
var searchTerm = this.getParameter('sysparm_search');
gs.info('Eric - the record it is '+recordId);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', recordId);
gr.query();
while (gr.next()) {
var fileName = gr.getValue('file_name') || '';
gs.log('Eric - the record it is '+recordId);
gs.log('Eric - the attahced file name is '+gr.file_name);
if (fileName.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1) {
return 'true';
}
}
return 'false';
},
type: 'AttachmentUtils'
}); This doesn't appear to be working.
I've tried adding in gs messages and nothing appears, nor am i getting any error messages.
So I'm not sure it's even being called.
Suggestions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
44m ago
Hi @EricG2 ,
Your GlideAjax call points to:
var ga = new GlideAjax('AttachmentUtils2');
But your Script Include's type is:
type: 'AttachmentUtils'
did you try fixing this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
34m ago
OOps. Thanks for the catch. Saw there was another AttachmentUtils - still isnt working.
var AttachmentUtils2 = Class.create();
AttachmentUtils2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRequiredAttachment: function() {
var recordId = this.getParameter('sysparm_sys_id');
var searchTerm = this.getParameter('sysparm_search');
gs.info('Eric - the record it is '+recordId);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', recordId);
gr.query();
while (gr.next()) {
var fileName = gr.getValue('file_name') || '';
gs.log('Eric - the record it is '+recordId);
gs.log('Eric - the attahced file name is '+gr.file_name);
if (fileName.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1) {
return 'true';
}
}
return 'false';
},
type: 'AttachmentUtils2'
});Thank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10m ago
so you want to get the file name and validate?
but what if user gives file name as valid but adds wrong file type?
Did your customer agree with the file type as well?
💡 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 || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
