OnLoad Client Script is not working with GlideAjax

pranavagarw
Tera Expert

OnLoad Client Script:

function onLoad() {

    var ga = new GlideAjax('global.TestingClientSide');
    ga.addParam('sysparm_name', 'onLoadTest');

    ga.getXMLAnswer(callbackFunc);
    function callbackFunc(resp) {
		alert(resp);
        g_form.setValue('description', resp);
    }
}

 

Script Include(Glide AJAX enabled):

var TestingClientSide = Class.create();
TestingClientSide.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	onLoadTest: function() {
		return "hello";
	},

    type: 'TestingClientSide'
});

 

It is always returning null, I have tried both async, sync methods but is not working either way. Simple thing but dont know why it is not working.

1 ACCEPTED SOLUTION

pranavagarw
Tera Expert

I have debugged this, and after trying all the possible solutions I found that everything is working fine.

Just, the function name 'onLoadTest' is causing the issue, because ServiceNow thought it as some OOTB keyword.😑
I have changed the function name to onLodTest/onLoaTest/xyz/etc. all is working fine.

Thank you for all the replies and help. 😄

View solution in original post

7 REPLIES 7

Ravi Gaurav
Giga Sage

Hi @pranavagarw 

 

Check if the below  you have done it

 

 

  • It’s in the Global scope (not application-scoped unless you allow cross-scope access).

  • It’s Client Callable (checkbox checked).

  • The function name matches exactly.

Here’s a correct version:

var TestingClientSide = Class.create();
TestingClientSide.prototype = Object.extendsObject(AbstractAjaxProcessor, {

onLoadTest: function() {
return "hello";
},

type: 'TestingClientSide'
});

--------

 

Here’s a working onLoad Client Script version:

function onLoad() {
var ga = new GlideAjax('TestingClientSide');
ga.addParam('sysparm_name', 'onLoadTest');
ga.getXMLAnswer(function(response) {
if (response) {
alert(response);
g_form.setValue('description', response);
} else {
alert('No response received');
}
});
}

 

Notes:

  • Don’t include 'global.' before the Script Include name in GlideAjax ('TestingClientSide' is enough).

  • The callback function must handle the asynchronous response.

  • Ensure there’s no syntax mismatch (e.g., curly braces or missing semicolons).

 

 

ChallaR
Giga Guru

Hi @pranavagarw ,

1. Script Include Must Be Global and Client Callable

Make sure your Script Include is:

  • Active
  • Client Callable (checkbox must be checked)
  • Global (or accessible from the scope you're calling it from)

Correct Script Include:

var TestingClientSide = Class.create();
TestingClientSide.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    onLoadTest: function() {
        return "hello";
    },

    type: 'TestingClientSide'
});

Note -

Make sure:

  • The name of the Script Include is exactly TestingClientSide
  • It is Client Callable

2. Client Script Must Match the Script Include Name

Your client script is mostly correct, but let’s ensure it’s clean:

 

function onLoad() {
    var ga = new GlideAjax('TestingClientSide'); // No 'global.' prefix
    ga.addParam('sysparm_name', 'onLoadTest');
    ga.getXMLAnswer(function(response) {
        var answer = response;
        alert(answer);
        g_form.setValue('description', answer);
    });
}

 

  • Do not prefix 'global.TestingClientSide' unless you're in a scoped app and the Script Include is in the global scope.
  • If you're in a scoped app, ensure the Script Include is in the same scope or accessible.

3. Check Scope Compatibility

If you're working in a scoped application, and your Script Include is in the global scope, you must prefix it like this:

var ga = new GlideAjax('global.TestingClientSide');

 

Debugging Tips:

  • Use console.log() instead of alert() for better debugging.
  • Check browser console for errors.
  • Use gs.info() in the Script Include to verify it's being called.

Please Mark as complete and close the thread if this is helpful.

Thanks,

Rithika.ch

 

pranavagarw
Tera Expert

I have debugged this, and after trying all the possible solutions I found that everything is working fine.

Just, the function name 'onLoadTest' is causing the issue, because ServiceNow thought it as some OOTB keyword.😑
I have changed the function name to onLodTest/onLoaTest/xyz/etc. all is working fine.

Thank you for all the replies and help. 😄