- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Well - copilot's a wonderful thing.
I redid the Client script and Script include. It's working now.
Client Script
unction onSubmit() {
var mgrSubmit = g_form.getValue(g_form.setValue('submitted_by_manager');
if(mgrSubmit==true){
var ga = new GlideAjax('AttachmentValidator');
ga.addParam('sysparm_name', 'hasManagerApproval');
ga.addParam('sysparm_table', 'sc_cart_item');//g_form.getTableName());
ga.addParam('sysparm_sys_id', g_form.getValue('sysparm_item_guid'));
var answer = ga.getXMLWait();
var result = answer.documentElement.getAttribute("answer");
g_form.setValue('submitted_with_manager_approval',true);
if (result !== 'true') {
g_form.addErrorMessage('You need to attach an approval email from your manager before you can submit this request. Make sure the name of the file includes "Manager Approval".');
return false;
}
return true;
}}
Script Include
var AttachmentValidator = Class.create();
AttachmentValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasManagerApproval: function() {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', this.getParameter('sysparm_table'));
gr.addQuery('table_sys_id', this.getParameter('sysparm_sys_id'));
gr.query();
while (gr.next()) {
if (gr.file_name.toString().toLowerCase().indexOf('manager approval') > -1) {
return true;
}
}
return false;
},
type: 'AttachmentValidator'
});
Not sure what the difference is, but it's working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @EricG2
I think the issue is due to usage of the getXMLWait() as this doesn't work in service portal. Please refer to these link.
Hope that Helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Well - copilot's a wonderful thing.
I redid the Client script and Script include. It's working now.
Client Script
unction onSubmit() {
var mgrSubmit = g_form.getValue(g_form.setValue('submitted_by_manager');
if(mgrSubmit==true){
var ga = new GlideAjax('AttachmentValidator');
ga.addParam('sysparm_name', 'hasManagerApproval');
ga.addParam('sysparm_table', 'sc_cart_item');//g_form.getTableName());
ga.addParam('sysparm_sys_id', g_form.getValue('sysparm_item_guid'));
var answer = ga.getXMLWait();
var result = answer.documentElement.getAttribute("answer");
g_form.setValue('submitted_with_manager_approval',true);
if (result !== 'true') {
g_form.addErrorMessage('You need to attach an approval email from your manager before you can submit this request. Make sure the name of the file includes "Manager Approval".');
return false;
}
return true;
}}
Script Include
var AttachmentValidator = Class.create();
AttachmentValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasManagerApproval: function() {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', this.getParameter('sysparm_table'));
gr.addQuery('table_sys_id', this.getParameter('sysparm_sys_id'));
gr.query();
while (gr.next()) {
if (gr.file_name.toString().toLowerCase().indexOf('manager approval') > -1) {
return true;
}
}
return false;
},
type: 'AttachmentValidator'
});
Not sure what the difference is, but it's working.
