abort onSubmit client script not working in Service Portal

michelek87
Kilo Expert

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

5 REPLIES 5

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Michele



Probably this is a stupid observation but If i'm not wrong the command should be g_form.submit not g_form.submitted.



http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#submit



Maybe this is the reason why it doesn't work.



ps. As visible documentation/client_scripting.md at master · service-portal/documentation · GitHub g_form.submit is allowed on the service portal.



I hope this will help/answer your question and if it does please mark it



Cheers


R0b0


Zeeshan Khan1
Kilo Guru

Hi,



Adding to what R0b0 suggested, I think the other issue could be the asynchronous ajax call because the form might be submitted before the callback function is executed.



Since you cannot use synchronous calls in service portal, you can perform the validation in onChange client script (using async ajax) and use some global variable that will allow you to check the variable value in onSubmit client script.



Alternatively you can refer the below thread providing different solution to this issue -


Require attachment for catalog item in Service Portal


amlanpal
Kilo Sage

Hi Michele,



Generally you can not use return false in Asynchronous GlideAjax call. You have to use the Synchronous GlideAjax in this case. Again calling Synchronous GlideAjax is not recommended. For technical reference you may refer the below threads:


1. Examples of asynchronous GlideAjax


2. Examples of synchronous GlideAjax


3. return false not working in client script



However the exact issue is been addressed over here Require attachment for catalog item in Service Portal . I believe this is what you are looking for. All you need to leverage your existing script as per the mentioned thread.



I hope this helps. Please mark correct/helpful based on impact


Hi michelek87,



Were you able to have a look at my response?


If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.


If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View