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
03-02-2018 08:49 AM
You can't use callback function in a onSubmit Script. You have to use getXMLWait(). I dont think there is any other option.
You can add you script to onChange script. But still while submitting, you need to perform validation. We use getXMLWait() for all onSubmit script for validation. No response time is not bad if we use getXMLWait() also.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2018 07:23 AM
Try returning a value to the main body of the script and then canceling the submission from there based on that value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2018 07:30 AM
Hi cwesley,
Can you give me an example?
Thanks,
Laurie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2018 07:56 PM
Hello Laurie,
Did you solve your problem? I have the same case and I am looking for the solution.
Tnks in advance,
Mónica A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2018 09:07 PM
Hi,
First of let me explain the issue which is over here and then i will let you know what i did to solve this issue.
Today I was working on the issue of onSubmit client script which was using getXMLWait() which make synchronous calls to the server waiting for the response from the server. Since this method is deprecated in service portal we cannot use this anymore.
An alternative for this was to use getXML() or getXMLAnswer() which makes asynchronous calls, but these methods do not work with onSubmit simply because the record will be getting submitted without waiting for server’s response. This does not serve our purpose.
Hence, in order to solve this i created 2 separate scripts:
1. Which will run only on UI type Desktop (this will be only for catalog)
2. Which will run on UI Type Mobile/Portal (this will be for the portal)
The first script will be your normal script which you use with getXMLWait()
The second script is some what like this:
function onSubmit() {
//for service portal
if(!window){
if(g_form.getValue('es_request_type') == 'cancellation'){
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var usr = g_form.getValue('es_requested_for');
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id',usr);
grUser.query(function(){
if(grUser.next()){
var lan = grUser.preferred_language;
}
var queryDpt = new GlideAjax("userDepartment");
queryDpt.addParam('sysparm_name', 'hasDepartments');
queryDpt.addParam('sysparm_sys_id', usr);
queryDpt.getXML(getDept);
function getDept(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//var resp = queryDpt.getAnswer();
if(answer == 1 && g_form.getValue('es_department') == ''){
g_form.setMandatory('es_department',true);
if(lan == 'es'){
alert('Debe seleccionar un departamento');
}
else if (lan == 'fr'){
alert('Vous devez choisir un département');
}
else if (lan == 'pt'){
alert('Deve selecionar um departamento');
}
else {
alert('You need to choose a department');
}
return false;
}
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
});
return false;
}
return true;
}
}
Note: This script is use to find the user departments if the user has department, make the field mandatory.
GlideRecord is used with a callback function since simple glideRecord is not supported in Portal.
Please tweak your code accordingly
Regards,
Aakash Shah