- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 04:38 PM
Hi, I am new to ServiceNow, have a Xanadu PDI instance and there is a very simple case that I would like to try, but it does not work.
When a Problem record is loaded, a client script (CS) calls a script include (SI) with the Problem number as the passed parameter. The SI simply returns the passed parameter, and the CS displays it as an infoMessage.
The steps I took:
1. Using Creator I created an application "abccompany" with global scoop.
2. Created a CS:
function onLoad() {
var problemNumber = g_form.getValue('problem.number');
var ga = new GlideAjax('Mdssechofromserver');
ga.addParam('sysparm_name', 'echoParameter');
ga.addParam('sysparm_num', problemNumber);
alert('In client script');
ga.getXMLAnswer(echoCallBack);
function echoCallBack(response) {
alert('In callBack of client script');
var answer =
response.responseXML.documentElement.getAttribute('answer');
g_form.addInfoMessage(
'This is the problem.number echo from server: ' + answer);
}
}
To resolve above message:
Checked the "Isolation property" flag of CS.
I created the app. property "glide.script.block.client.globals" and set it to false.
3. Created SI:
var Mdssechofromserver = Class.create();
Mdssechofromserver.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
echoParameter: function() {
var problemNumber = this.getParameter('sysparm_num');
return problemNumber;
},
type: 'Mdssechofromserver'
});
Alerts from JavaScript are displayed, but the infoMessage is not.
If I put an alert statement behind response.responseXML... like this
var answer =
response.responseXML.documentElement.getAttribute('answer');
alert(answer);
This alert is not executed. Is it normal that the callback is called even if the SI failed or was not called at all?
There are several examples from earlier ServiceNow versions for calling SI from CS, but in Xanadu, they do not work because the settings are slightly different.
I appreciate all the help.
mdaniel
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 11:18 AM - edited 03-05-2025 11:27 AM
Hi @MihályD
Can you please try the below code and check whether you're getting the response?
Client script
function onLoad() {
//Type appropriate comment here, and begin script below
var problemNumber = g_form.getValue('number');
var ga = new GlideAjax('global.ScriptInclude');
ga.addParam('sysparm_name', 'echoParameter');
ga.addParam('sysparm_num', problemNumber);
ga.getXML(echoCallBack);
function echoCallBack(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.addInfoMessage('This is the problem number echo from server: ' + answer);
}
}
Script include seems okay.
Attaching the snippet for reference.
Also, I would like to hear what role you selected while creating a new script include?
Please mark this as helpful if this resolves your query.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 11:05 AM
Hi @MihályD ,
Do not use below code when you are using getXMLAnswe(). Use the below code when you are using getXML()
var answer = response.responseXML.documentElement.getAttribute('answer'); alert(answer);
getXML() returns the entire object response with extra option and the actual output is stored under the attribule answer.
getXMLAnswe() returns only the output in a string format.
Mark this helpful/ Accept the solution if this helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 12:37 PM
Hello @MihályD
Try this onLoad Client script:
function onLoad() {
var problemNumber = g_form.getValue('number'); // Corrected field name
if (!problemNumber) {
g_form.addErrorMessage("Problem number is empty.");
return;
}
var ga = new GlideAjax('Mdssechofromserver');
ga.addParam('sysparm_name', 'echoParameter');
ga.addParam('sysparm_num', problemNumber);
g_form.addInfoMessage("Sending request to Script Include with problem number: " + problemNumber);
ga.getXMLAnswer(function(answer) {
g_form.addInfoMessage('This is the problem number echo from server: ' + answer);
});
}
To learn more about getXML & getXMLAnswer method refer to:
Note:
- Do NOT use response.responseXML.documentElement.getAttribute('answer'); when using getXMLAnswer().
- getXMLAnswer directly returns the processed value
- If XML parsing is required, use getXML() instead
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 11:18 AM - edited 03-05-2025 11:27 AM
Hi @MihályD
Can you please try the below code and check whether you're getting the response?
Client script
function onLoad() {
//Type appropriate comment here, and begin script below
var problemNumber = g_form.getValue('number');
var ga = new GlideAjax('global.ScriptInclude');
ga.addParam('sysparm_name', 'echoParameter');
ga.addParam('sysparm_num', problemNumber);
ga.getXML(echoCallBack);
function echoCallBack(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.addInfoMessage('This is the problem number echo from server: ' + answer);
}
}
Script include seems okay.
Attaching the snippet for reference.
Also, I would like to hear what role you selected while creating a new script include?
Please mark this as helpful if this resolves your query.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 12:04 PM
Hi @_Gaurav,
Thank you for your quick answer, but as you can see below on the alert pop up
the Problem number field reference
g_form.getValue('problem.number');
is correct, because it returns the correct Problem number ( it was taken from the HTML source of the form).
I think the CS and SI naming conventions follows the related rules, so why change them?
The "global" prefix
new GlideAjax('global.Mdssechofromserver');
does not solve the problem.
Regards
mdaniel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 06:20 AM - edited 03-06-2025 06:33 AM
Hi @MihályD
Can you check adding gs.info in the SI and whether you're getting the problem number in the SI?
Also, what role have you selected while creating the SI?
You can also check replacing
var problemNumber = g_form.getValue('problem.number');
with this
var problemNumber = g_form.getValue('number');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 11:05 AM
Hi @MihályD ,
Do not use below code when you are using getXMLAnswe(). Use the below code when you are using getXML()
var answer = response.responseXML.documentElement.getAttribute('answer'); alert(answer);
getXML() returns the entire object response with extra option and the actual output is stored under the attribule answer.
getXMLAnswe() returns only the output in a string format.
Mark this helpful/ Accept the solution if this helps you.