"return false;" not working onSubmit in Service Portal

patricklatella
Mega Sage

Hi all,

I've got an onSubmit catalog client script to present a modal to redirect the user from our CSM portal to our old CMS portal.  The logic determines if the user has Class = "sys_user" or Class = "customer_contact".  If the user's Class is "sys_user" I want to show the popup and then block the form from being submitted.  I've got the popup showing but the "return false;" is not working.  Thanks!

Here's my client script:

function onSubmit() {
//if logged in user is not a Contact, direct them to the ESS portal

var user = g_form.getValue('logged_in_user');
var ga = new GlideAjax('CSMContactClassLookup');
ga.addParam('sysparm_name','getClass');
ga.addParam('sysparm_user', user);
ga.getXML(getClassResponse);

function getClassResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('answer is '+answer);
if(answer == 'sys_user'){
spModal.open({
title: 'Got an issue?',
message: 'To report an issue you need to use a different form in our ESS portal. <p><p>Click the button below to redirect to the correct form.',
buttons: [
{label:'Take me to the correct form', primary: true},
{label:'Stay on this page', cancel: true}
]
}).then(function(){
location.href="ess/main_content.do?sysparm_content_url=com.glideapp.servicecatalog_cat_item_view.do%3fsysparm_id%3d3f1dd0320a0a0b99000a53f7604a2ef9";
});
return false;//this is not working, the form still submits
}
}
}

 

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You should change this to an onChange client script. GlideAjax with getXML wont work in onSubmit client script, since it is asynchronous. And getXMLWait() wont work on Service Portal. So I would suggest adding it on onChange and clear the value 'logged_in_user'


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

Ankush16
Kilo Guru

Hi patricklatella,

Can you confirm your UI type selected as All ?

Also check your  browser console in chrome any error your are seeing ? 

Also you are allowing user to fill form and then running your validation might be frustrating for user to full form and then be presented with info that its a wrong form 🙂  

 

Hi Ankush, thanks for the response.  This is a unique use case where actually onLoad I present the user (if their Class is User) with the popup that redirects them to our CMS portal.  This is to prevent them from ignoring that popup and trying to submit the form anyway.  I agree that preventing them continuing with the form at all if it's the wrong form would be a better UX. 

The SN Nerd
Giga Sage
Giga Sage

GlideAjax makes an Asynchronous call to the server.

You need to use getXMLWait() for an onSubmit to make it synchronous.

Otherwise, it will just submit immediately.

ga.getXMLWait(getClassResponse);

If you are working out of application scope, getXMLWait isn't available and you will need to re-engineer your whole solution to not use GlideAjax.

You could do this by doing your GlideAjax on load and setting a scratchpad variable, then checking that variable onSubmit.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

thanks for the response Paul, yeah I think the issue is that this record producer is in the Customer Service application scope, so I'm thinking getXMLWait won't work.  I'll try another approach.