How to execute client callable script include in background script

Ujjwal1
Tera Contributor

hi all,

i want to test client callable script include. Is there any way to test this in background script.

 

sample code snippet:

var Userlocationfetch = Class.create();
Userlocationfetch.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    locationDetails: function() {
        var userID = this.getParameter("sysparm_user");
        var grUser = new GlideRecord('sys_user');
// logic here
//return location
 return JSON.stringify(location);
    },
    type: 'Userlocationfetch'
});

 

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Ujjwal1 ,

 

To execute a client callable script include from a background script, you can use the GlideAjax class.

Here is an example of how to do it:

 

// Instantiate the client callable script include
var userLocationFetch = new Userlocationfetch();

// Create a new GlideAjax object
var ga = new GlideAjax('Userlocationfetch');

// Set the name of the client callable method to call
ga.addParam('sysparm_name', 'locationDetails');

// Set the user parameter to pass to the method
ga.addParam('sysparm_user', 'user_sys_id_here');

// Execute the AJAX call and get the response
var response = ga.getXMLWait();

// Parse the response as JSON
var location = JSON.parse(response.responseText);

// Do something with the location object

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

Sandeep Rajput
Tera Patron
Tera Patron

Following changes make a script include usable from both client and server side.

 

Script Include

var Userlocationfetch = Class.create();
Userlocationfetch.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    locationDetails: function(userSysID) {
        var userID = this.getParameter("sysparm_user")?this.getParameter("sysparm_user"):userSysID;		
        var grUser = new GlideRecord('sys_user');
		var location = '';
		if(grUser.get('sys_id',userID)){
			location = grUser.getDisplayValue('location');
		}
// logic here
//return location
 return location;
    },
    type: 'Userlocationfetch'
});

Screenshot 2023-04-07 at 3.08.17 PM.png

Here is how you are going to call it in the background script.

var user = new global.Userlocationfetch();    
gs.print(user.locationDetails(gs.getUserID()));

 

Screenshot 2023-04-07 at 3.08.51 PM.png

have you passed sys_id of that which user