GlideAjax is not defined when called from UI Action

sudhakar_dasari
Tera Contributor

Hi All,

I created a Script Include and trying to call it using UI Action. 

Both UI Action and Script Include are in the same scope but not global. Script Include has the Client Callable enabled and also tried to invoke it from UI Action by prepending the scope name before the Script Include.

The button click fails with error as GlideAjax is not defined.

Attempted the same by creating a UI Macro and surprisingly, there is no error but, the call to Script Include is also not happening as I tried logging the flow (gs.info() as it is a scoped application) and didnt see anything in the System Logs.

 

Script Include code:

var REST_API_CALL = Class.create();
REST_API_CALL.prototype = {
initialize: function() {},

getAccessToken: function() {
var tokenEndpoint = 'https://.../oauth2/token'; // Replace with token URL
var clientId = 'xxxxxxxxxxxxxxxx'; // Replace with your Client ID
var clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your Client Secret
var username = 'xxxxxxxxxxx'; // Replace with API username
var password = 'xxxxxxxxxxxxxxxx'; // Replace with API password

var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint(tokenEndpoint);
restMessage.setHttpMethod('POST');

// Set headers
restMessage.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

// Set body with grant type password parameters
var requestBody =
'grant_type=password' +
'&username=' + encodeURIComponent(username) +
'&password=' + encodeURIComponent(password) +
'&client_id=' + encodeURIComponent(clientId) +
'&client_secret=' + encodeURIComponent(clientSecret);

restMessage.setRequestBody(requestBody);

try {
// Execute the request
var response = restMessage.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();
var responseJSON = JSON.parse(responseBody);

if (httpStatus === 200) {
return responseJSON.access_token;
} else {
gs.error('Error fetching access token. Status: ' + httpStatus + ', Response: ' + responseBody);
return null;
}
} catch (ex) {
gs.error('Error during token retrieval: ' + ex.message);
return null;
}
},

getData: function() {
var accessToken = this.getAccessToken();
if (!accessToken) {
gs.error('Failed to retrieve access token. Aborting API call.');
return;
}

var apiEndpoint = 'https://xxxxxxxxxxxxxxxxxxxx'; // Replace with target API URL
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint(apiEndpoint);
restMessage.setHttpMethod('GET');

// Set Authorization header
restMessage.setRequestHeader('Authorization', 'Bearer ' + accessToken);
restMessage.setRequestHeader('Content-Type', 'application/json');

try {
// Execute the API request
var response = restMessage.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();
var responseJSON = JSON.parse(responseBody);

return {
status: httpStatus,
body: responseJSON
};
} catch (ex) {
gs.error('Error during API call: ' + ex.message);
return {
status: 'error',
message: ex.message
};
}
},

type: 'REST_API_CALL'
};

 

UI Macro:

function execute_getdata(inputs, outputs) {
    var helper = new GlideAjax('x_scope.REST_API_CALL');        
    helper.addParam('sysparm_action', 'getData');
    console.log('before getXML');
    helper.getXML(callback);
    console.log('after getXML');
}
function callback(response){
    console.log('I am here');
    console.log(response.responseXML.documentElement.getAttribute("answer"));
    console.log('I am done');
}

 

UI Action:

var gaj = new GlideAjax('REST_API_CALL');        
gaj.addParam('sysparm_name', 'getData');
var result = gaj.getXML(function(response) {
    console.log(response);
});

 

Appreciate any help here.

2 ACCEPTED SOLUTIONS

Runjay Patel
Giga Sage

Hi @sudhakar_dasari ,

 

you can call using scope.script includes name, as per your code in ui action you have not added scope. 

 

View solution in original post

Ranjane_Omkar
Kilo Sage

Hi @sudhakar_dasari,

 

If you are calling the script include from any other scope you need to call it by using API name of script include instead of script include name as highlighted in below screenshot.

Ranjane_Omkar_1-1732607615771.png

 

Regards,

----

If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

 

View solution in original post

2 REPLIES 2

Runjay Patel
Giga Sage

Hi @sudhakar_dasari ,

 

you can call using scope.script includes name, as per your code in ui action you have not added scope. 

 

Ranjane_Omkar
Kilo Sage

Hi @sudhakar_dasari,

 

If you are calling the script include from any other scope you need to call it by using API name of script include instead of script include name as highlighted in below screenshot.

Ranjane_Omkar_1-1732607615771.png

 

Regards,

----

If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.