Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

karthikdurga
Mega Expert

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.