- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2022 07:29 AM
I'm developing a script that determines the value of a field in the catalog client script and makes the submission an error.
I would like to pass a parameter to the script include so that the existing record and the record to be created do not have the same value.
However, the client script "getXMLAnswer" did not return the return value to "test_response".
does anyone know how?
In the script include, judge based on the argument of "param_1" and return "true" or "false".
function onSubmit() {
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
//Pass the status as an argument to see if it is duplicated
ga.addParam('param_1', "status");
//"true" if the status is duplicated
//I expect it to return "false" if it's not a duplicate.
var test_response = ga.getXMLAnswer(callback);
//I want to determine and process the return value of "getXMLAnswer"! !
if (test_response == false) {
alert(response);
}
}
function callback(response) {
alert(response);
return response;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 09:55 AM
As you are using getXMLWait() on portal which is no longer supported, you might have received an error message in your browser console. As for getXMLAnswer() or getXML() both are asynchronous in nature so they cannot validate/stop submission using onSubmit().
I found a knowledge article How to do async validation in an onsubmit client script , which might help in resolving your requirement. I tried to implement it, and it was working fine.
What you can do is create 2 onSubmit() Catalog Client Scripts one for Desktop and second for Mobile/Service Portal to tackle your requirement.
Desktop
function onSubmit() {
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
ga.getXMLWait();
var test_response = ga.getAnswer();
if (test_response.toString() == 'false') {
g_form.addInfoMessage('Response: ' + answer);
g_form.addInfoMessage('Response Type: ' + typeof answer);
return false;
}
}
See the image for reference.
Mobile / Service Portal
function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.addInfoMessage('Response: ' + answer);
g_form.addInfoMessage('Response Type: ' + typeof answer);
if (answer.toString() == 'false') {
g_form.addErrorMessage('Cannot Submit');
return false;
} else {
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
});
return false;
}
See the image for reference.
Hopefully, this will help you resolve your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2022 07:43 AM
Hi,
It would be better if you could provide the script include as well. Another point I would say is that try using getXMLWait() in onSubmit() client script.
ga.getXMLWait();
var test_response = JSON.parse(ga.getAnswer());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2022 06:49 PM
Thank you for answering
The script include I created is for testing, so I always return "false".
If the catalog client script can receive the return value of the script include, there is no problem.
var test_script = Class.create();
test_script.prototype = Object.extendsObject(AbstractAjaxProcessor, {
test: function() {
//Since it is a test script, "false" is always returned.
return false;
},
type: 'test_script'
});
Since the return value is not in JSON format, I tried to modify it as follows, but it did not work.
ga.getXMLWait();
var test_response = ga.getAnswer();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 01:39 AM
The reason you are not getting response in below statements is because you are using getXMLAnswer(callback) which is asynchronous in nature, means the statements below var test_resposne = ga.getXMLAnswer(callback); are getting executed before you are getting response from callback function.
One more thing is that you are trying to compare test_response with boolean in test_response == false statement, but in reality you are getting string 'false' from script include though you are returning boolean false from there. The reason for this could be XML functions.
//"true" if the status is duplicated
//I expect it to return "false" if it's not a duplicate.
var test_response = ga.getXMLAnswer(callback);
//I want to determine and process the return value of "getXMLAnswer"! !
if (test_response == false) { alert(response); }
So the first approach to tackle this is to put those statements in callback function. See the below Client Script and Script Include for reference.
Client Script
function onSubmit() {
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
//Pass the status as an argument to see if it is duplicated
ga.addParam('param_1', "status");
//"true" if the status is duplicated
//I expect it to return "false" if it's not a duplicate.
var test_response = ga.getXMLAnswer(callback);
}
function callback(response) {
alert(response);
//I want to determine and process the return value of "getXMLAnswer"! !
if (response == 'false') {
alert('False: ' + response);
}else{
alert('Undefined: ' + response);
}
return response;
}
Script Include
var test_script = Class.create();
test_script.prototype = Object.extendsObject(AbstractAjaxProcessor, {
test: function() {
//Since it is a test script, "false" is always returned.
return false;
},
type: 'test_script'
});
Another approach would be to use getXMLWait() as below.
Client Script
function onSubmit() {
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
//Pass the status as an argument to see if it is duplicated
ga.addParam('param_1', "status");
//"true" if the status is duplicated
//I expect it to return "false" if it's not a duplicate.
ga.getXMLWait();
var test_response = ga.getAnswer();
//I want to determine and process the return value of "getXMLAnswer"! !
if (test_response == 'false') {
alert('False: ' + test_response);
}else{
alert('Undefined: ' + test_response);
}
}
Script Include
var test_script = Class.create();
test_script.prototype = Object.extendsObject(AbstractAjaxProcessor, {
test: function() {
//Since it is a test script, "false" is always returned.
return false;
},
type: 'test_script'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 07:07 AM
Thank you for your kind explanation!
This is the test result using [getXMLWait()].
You are not handling return values properly.
This is a test of "getXMLAnswer(callback)".
I modified the script slightly to display the return value. "alert (test_response);"
function onSubmit() {
var ga = new GlideAjax('test_script');
ga.addParam('sysparm_name', 'test');
//Pass the status as an argument to see if it is duplicated
ga.addParam('param_1', "status");
//"true" if the status is duplicated
//I expect it to return "false" if it's not a duplicate.
var test_response = ga.getXMLAnswer(callback);
alert(test_response);
}
function callback(response) {
alert(response);
//I want to determine and process the return value of "getXMLAnswer"! !
if (response == 'false') {
alert('False: ' + response);
}else{
alert('Undefined: ' + response);
}
return response;
}
This didn't work either. .