The CreatorCon Call for Content is officially open! Get started here.

How to prevent a form from being submitted in an onsubmit client script with a getXML() function?

Johannes Mweli
Giga Guru

Hi ServiceNow Community Developers

 

I have the following use case: I am in a scoped application. I am using the onsubmit client script that has the getXML() function. There are fields on the form that I am submitting that needs to be populated by the response of the getXML() function before the form can be submitted. The issue I am running into though is that since getXML() is async it does not wait for the response to come back from the server, it issues a call to the server and then goes ahead and submit the form before the response comes back. As a result of this behavior some fields are submitted without getting populated.

 

Would you guys please advise if there is a way I can overcome this. In other words is there a way to pause the form submission until the fields that needs to populated by the response  from getXML() function have been populated.

 

Kindly advise please

 

Thanks,

Johannes

1 ACCEPTED SOLUTION

@Johannes Mweli  SN has created a support article explaining this and how to make async call in onsubmit

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579 

 

NOTE: This does not work in catalog client script, as per my discussion with HI support.


Raghav
MVP 2023
LinkedIn

View solution in original post

5 REPLIES 5

kaushal_snow
Mega Sage

Hi @Johannes Mweli ,

 

Yes , you can prevent a form from submitting until the fields populated by a server call via getXML() are filled. Because getXML() is asynchronous, you need to block the initial submission, wait for the response, then re submit once done. Here’s how:

function onSubmit() {
// If flag says validation done, allow submit
if (g_scratchpad._validated) {
g_scratchpad._validated = false; // reset if needed
return true;
}

// Otherwise, do async validation via GlideAjax
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'validateSomething');
ga.addParam('sysparm_some_field', g_form.getValue('some_field'));
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
// mark that validation passed
g_scratchpad._validated = true;
// re-submit form via saved action
var action = g_form.getActionName();
if (typeof g_form.orderNow !== 'undefined') {
g_form.orderNow();
} else {
g_form.submit(action);
}
} else {
g_form.addErrorMessage("Validation failed: please correct fields");
}
});

// prevent initial submit
return false;
}

Please note: getXMLWait() is synchronous and delays submission until server responds. But it's not supported in Service Portal and discouraged generally....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/