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
11-22-2018 05:54 AM
Hey,
As @David Dubuis has mentioned - you cannot sent synchronous AJAX requests with GlideAjax anymore.
Furthermore, synchronous AJAX, is a bad practice altogether as it freezes the page for the user until the response has been received. For bigger data this would be a really bad user experience. The correct way to handle client-side validation is using onChange validators.
However, if you REALLY want to do it synchronously (NOT RECOMMENDED!), you could create a Scripted REST Resource to simulate what your Client Callable Script Include is doing and from the client (onSubmit function) - send the request to the created Scripted REST Resource and wait for response (synchronous) with the JavaScript native XMLHttpRequest API.
For example:
//Scripted REST Resource
(function(/*RESTMessageV2*/ request, /*RESTResponseV2*/ response) {
//read parameters from the request
var requestData = request.body.data;
var result = false; //validation result
var statusCode = 0; //response status code
try {
//apply your logic here
//set result
statusCode = 201;
result = true;
} catch(err) {
statusCode = 400;
}
response.setStatusCode(statusCode);
response.setBody({
answer: result
});
})(request, response);
And call it on the onSubmit client script:
function onSubmit() {
//get your form variables
//request parameters
var endpoint = 'YOUR_SCRIPTED_REST_RESOURCE_URL';
//fill in the parameters you will read from the server
var requestBody = JSON.stringify({
//fill them in here as key: value pairs
});
//send validation request
var req = new XMLHttpRequest();
//this is your callback
req.onreadystatechange = function() {
if(req.readyState == 4) {
return JSON.parse(req.responseText).answer == true;
}
}
req.open('POST', endpoint, true);
req.send(requestBody);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2018 12:00 PM
Will this code work in a widget on the Service Portal?
Thanks,
Laurie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 12:14 PM
If “Date of joining” is in employees data field is selected as past date, then display the error message that “It should be future date, not allowed to select past date.”
