Server-side AJAX 

Now that we have seen the client aspect of an AJAX call, let's have a look at the the server-side code that is required to make this work.

As mentioned before, we need to call the script include the same name as our AJAX call and make sure it contains a function with the name in the sysparm_name parameter. We also need to ensure we make the script include the client callable for the AJAX call to work, and we can do this by checking the client callable tick box on the script include:

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

getUserLocation: function getUserLocation() {
var userRecord = new GlideRecord('sys_user');
userRecord.get(this.getParameter('sysparm_user_id'));
return userRecord.location.getDisplayValue();

}

});

Here, we are using the getUserLocation function to return the caller's location back to the client. By using GlideRecord and the get method to obtain the user record, we can then return the location from that user record back to the client. We are using the display value to display to the user on the client; otherwise, we simply display the location record sys_id, which does not mean much to an end user.

By using AJAX calls, we can pass back single values like in our example, or multiple values if needed. An array is a good way to pass multiple values back, but you can use other methods, too, as the response is an XML document.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.192