Preblem with calling script include in Xanadu

MihályD
Tera Contributor

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:

MihlyD_0-1741131521110.png

 

 

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.

 

MihlyD_2-1741132546077.png

 

3. Created SI:

MihlyD_1-1741132173252.png

 

 

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

 

 

 

 

3 ACCEPTED SOLUTIONS

_Gaurav
Kilo Sage

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.
_Gaurav_0-1741202174458.png_Gaurav_1-1741202200694.png

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!


View solution in original post

Community Alums
Not applicable

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.

 

 

 

View solution in original post

Juhi Poddar
Kilo Patron

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

View solution in original post

5 REPLIES 5

_Gaurav
Kilo Sage

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.
_Gaurav_0-1741202174458.png_Gaurav_1-1741202200694.png

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!


MihályD
Tera Contributor

Hi @_Gaurav,

Thank you for your quick answer, but as you can see below on the alert pop up

MihlyD_0-1741203474443.png

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

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');

  

Community Alums
Not applicable

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.