onSubmit Client Script - return false not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2018 07:17 AM
Hello,
We have a variable set with an onSubmit client script for catalog items that checks various accounting detail information before an item is put into the shopping cart. I had to make changes to the script so it would work on the service portal. The script had a "getXMLWait()" which I had to change to "getXML()". So every thing is working fine except in the callback function, the "return false" is not stopping the submission. Here is the code.
The if statement with the alert('here i am') displays the alert, but it doesn't seem to stop the submission, and still puts the item in the cart. What is causing this?
function callBack(response){
var valResponse = response.responseXML.documentElement.getAttribute("answer");
alert(valResponse);
valResponse = valResponse.split(',');
alert(valResponse[0]+'---'+valResponse[1]);
if(valResponse[0] == '200'){
alert("enter"+valResponse[0] );
if(valResponse[1] == 'V'){
alert("validation passed");
}else{
alert("Please enter correct Accounting Details");
return false;
}
}
if(valResponse[0] == '500'){
if(valResponse[1] != 'V'){
alert('here is am');
alert("Please enter correct Accounting Details");
return false;
}
}
else{
alert('in last else');
alert("Please enter correct Accounting Details");
return false;
}
}
}
Thank you,
Laurie
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2018 10:15 PM
I think the best solution would be an onChange client script. Have the callback function blank out the field if the server response is invalid. That way since the variable is mandatory and has been blanked out, it won't let you submit.
If you really have to have it onSubmit, do an onChange client script that sets a flag in g_scratchpad to true or false in your callback function, then have your onSubmit check the flag, so there's no AJAX in your onSubmit.
So for the second case, your callback function in your onChange would be:
function callBack(response){
var valResponse = response.responseXML.documentElement.getAttribute("answer");
//or 'valResponse = response;' if you used getXMLAnswer() instead of getXML()
valResponse = valResponse.split(',');
g_scratchpad.validDetails = false;
if(valResponse[0] == '200' || valResponse[0] =='500'){
if(valResponse[1] == 'V'){
g_scratchpad.validDetails = true;
}
}
}
Then have an onSubmit client script:
function onSubmit() {
if(!g_scratchpad.validDetails) {
alert("Please enter correct Accounting Details");
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2018 07:53 AM
Correct, it worked for me ... change the logic to onChange and remove the field value to force the user to correct it.
Chris, thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2018 06:29 AM
So I spoke with Nathan Firth, the person who developed the service portal, and he said the way you can get this to work is by using a javascript promise. Here is a definition of a promise:
A promise represents the eventual result of an asynchronous operation. It is a placeholder into which the successful result value or reason for failure will materialize.
I haven't gotten around to trying this, as I was assigned to work on a different item for the time being.
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2018 07:54 AM
For the moment, change the logic to onChange and remove the field value to force the user to correct it.
Thanks a lot Laurie!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2018 04:25 AM
You can find another solution in the following thread. By returning false for the submit Script on default and therefor canceling the transaction you can wait for the result and perform the action then. The downside is, that it might be possible for the user to change information in that time.
https://community.servicenow.com/community?id=community_question&sys_id=d9348729dbd8dbc01dcaf3231f961999
