How to evaluate if there is an attachment in the current form with an onSubmit Client Script?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 06:29 PM
Hey everyone,
I have been trying to test something out and find a way to abort a submission on an onSubmit client script. I only want it to abort submit and give and error message if there is no attachment in the form and the caller is "Chuck Farley" when the submit button is pressed. I have been using GlideAjax to do this and was wondering if y'all see something I need to change to make it work. I can't get the script include to execute. Does anyone have any thoughts? Am I just calling the function in the script include wrong? (my code is below) I have just been working with the incident table.
Client Script:
function onSubmit() {
//Check if the caller on the for is 'Chuck Farley'
if (g_form.getValue('caller_id') == '46c9e158a9fe198101d44d0d22cb640d') {
//GlideAjax to use a script include to get a GlideRecord on sys_attachment table
var ga = new GlideAjax('CheckAttachmentsIncident');
ga.addParam('sysparm_name', 'checkAttach');
ga.addParam('thisSysId', g_form.getUniqueValue());
ga.getXML(isAttached);
//x is the returned value from the isAttached function below
if (x == true) {
return true;
} else {
g_form.addErrorMessage('You need to have an attachment with the caller of "Chuck Farley"');
return false;
}
}
}
//function to interpret the results of the script include and see if there is an attachment
function isAttached(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
//if there was not an attachment on the from when submitted, don't let it submit and tell the user
if (answer) {
x = true;
return x;
} else {
//alert('You can not submit a form with caller "Ty Petty" without an attachment');
x = false;
return x;
}
}
Script Include:
var CheckAttachmentsIncident = Class.create();
CheckAttachmentsIncident.prototype = {
//Check to see if there is an attachment on the current form
checkAttach: function() {
gs.log('CheckAttachments function working right now')
var tableSysId = this.getParameter('thisSysId');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', tableSysId);
gr.query();
//if there is an attachment then return true
if (gr.next()) {
gs.log('CheckAttachments: attachment detected in form')
var json = new JSON();
var result = {
'attach': true
}
return json.stringify(result);
} else {
//if there is no attachment then return null
gs.log('CheckAttachments: attachment not found')
return null;
}
},
type: 'CheckAttachmentsIncident'
};
0 REPLIES 0