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.

Prevent form submission dependent on GlideAjax response in onSubmit Client Script

ruth3brown
Tera Contributor

I have the following onSubmit client script on the Incident table.  It is intended to prevent the user Resolving (state = 6) the incident if a linked Outage record does not have an end date.  If I set an Incident with an open Outage record to Resolved and save I see the Error Message below.  It appears at the top of the screen for a moment, then disappears and the Incident is saved as Resolved.  I am new to Glide Ajax and may be misunderstanding how to process the response. I expected the message to stay on the screen until the user closed it, and the that the record would not save. 

Any help welcome.

Many thanks

Ruth

 

function onSubmit() {
if(g_form.getValue('state') != 6) {
return;
}
var inc = g_form.getUniqueValue();
var ga = new GlideAjax('DfE_CheckOutages');
ga.addParam('sysparm_name', 'checkOutages');
ga.addParam('sysparm_incident', inc); //sys_id for INC0010847
ga.getXML(ajaxResponse);

function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true') {
g_form.addErrorMessage('There is at least one open Outage record linked to this Incident. Please enter the End date(s) before you resolve this Incident');
return false;
}
}
}

3 REPLIES 3

dave_edgar
Mega Guru

Hi @ruth3brown

 

Maybe try an alert rather then a message

alert('There is at least one open Outage record linked to this Incident. Please enter the End date(s) before you resolve this Incident');
return false;

 

Try:

function onSubmit() {

if(current.state.changesTo == 6) { //you may need to use incident_state
var inc = g_form.getUniqueValue();
var ga = new GlideAjax('DfE_CheckOutages');
ga.addParam('sysparm_name', 'checkOutages');
ga.addParam('sysparm_incident', inc); //sys_id for INC0010847
ga.getXML(ajaxResponse);

function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true') {
alert('There is at least one open Outage record linked to this Incident. Please enter the End date(s) before you resolve this Incident');
return false;
}}}}

 

Hope this helps

 

Dave

ruth3brown
Tera Contributor

Thanks Dave, I originally had an alert, it displays the message but still goes ahead and saves the Incident record.  I don't know how to prevent the Incident record from being submitted.  I found another post on a forum that suggested I should use getXMLWait instead.  I will look into it but thought it was not generally recommended.

Any other ideas welcome.

Kind Regards

Ruth

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


Use:

 

g_form.submitted = false;

return false;

 

Thanks,
Ashutosh Munot