abort onSubmit client script not working in Service Portal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2017 12:39 PM
I have a client script where I need to check if they have included an attachment and if not, pop up an alert and abort submission of form.
The alert pops up but the form still submits.
I have reviewed several community threads including the one about the callback function which I also tried.
This is the latest version of my script:
function onSubmit() {
var attGA2 = new GlideAjax("LaptopAttachmentHelper");
attGA2.addParam('sysparm_name', "checkAttachment");
attGA2.addParam('sysparm_userName', g_user.userName);
attGA2.getXML(attResponse2);
}
function attResponse2(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == "none"){
alert("Please attach DS-0132 before submitting form.");
g_form.submitted=false;
return false;
}
}
line 16 doesn't make any difference.
I am getting the alert but as soon as I click OK on the alert, the form is submitted in the portal.
Thanks,
Michele
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2017 07:29 PM
We had a similar requirement at our client. Return false statement is necessary but insufficient to to prevent form submission in onSubmit() function. Use synchronous glideAjax to achieve this. I provided code below that worked for me. Hope this helps.
Client Script:
-----------------------------------------------------------------------------
function onSubmit() {
var attGA2 = new GlideAjax("LaptopAttachmentHelper");
attGA2.addParam('sysparm_name', "checkAttachment");
attGA2.addParam('sysparm_userName', g_user.userName);
attGA2.getXMLWait();
var answer = attGA2.getAnswer();
if (answer == "none"){
alert("Please attach DS-0132 before submitting form.");
//g_form.submitted=false; //not needed
return false;
}
}
------------------------------------------------------------------------------
Code Changes in Script Include:
Use return answer; instead of msg.setAttribute() function to return answer from script include to client script.