- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
12-17-2021 08:04 PM - edited 12-27-2022 07:00 PM
The ability to call server-side functionality using a GlideAjax object on the client side requires only a few lines of code and is well documented in many web pages - for example in the Documentation Site. Nevertheless, many users have troubles with it, as can be observed from the numerous questions on the topic in the Community. One of the reasons is the difficulty in tracing the interaction between client and server, because of the asynchronous behavior normal debugging approaches fail here. If the call does not work as expected, it is impossible to know whether the issue is in the Script include on the server side or in the client script that is executed in the browser. |
|
To better isolate the root cause, it would be helpful if one could have a general test interface on the client side that allows the construction of arbitrary GlideAjax requests and makes the server response visible in an easy-to-understand way.
Requirements
User Interface
For easy usage, a form is required in which you can specify both the name of the server-side Script Include and the method to be executed. Furthermore, a user-friendly input of the function parameters and the corresponding values is needed. A very popular source of errors is misspelled parameter names. As much as possible should be caught here and the user be made aware accordingly.
Error handling
In the case that a request failed, the user should be informed accordingly with meaningful error messages.
Source Code Generation
After having configured and tested a successful request, the complete source code required for this invocation is to be generated, so it can be easily integrated into the own implementation.
Solution
Since I wanted to keep such a testing interface as simple as possible, I decided to implement a record producer that does not create a new record, but only serves to construct and submit a request out of the form values.
The high flexibility of this interface results from the use of a Multi Row Variable Set (MRVS), which allows specifying arbitrary parameters to be passed to the Script Include on server side.
Processing the response is the real advantage of that test client and technically seen there are 3 answer types which are expressed in different ways.
(1) Successful response with source code generation
Basically a server responds with a 2xx code for successful deliveries, but the GlideAjax API does not offer reading the status code. And you can't normally query a status message via the API either. But to simulate a successful response the value of "200" is inserted manually into the "Status Code" field and a message with the number of returned characters is filled into the field "Status Message". At the "Body" field the text content of the response is inserted. In case the text could be identified as a valid JSON, it is presented in a prettified version with indentations and line breaks.
At the end of the form, a source code snippet based on your inputs is generated. You can copy that source code to the clipboard and use it for your own implementation. That way, you can be sure to have a working version of your GlideAjax based request.
(2) Successful response with no method invocation
Unfortunately the response code is also "200" in case a respective method at the requested Script Include could not be found and thus was not invoked. To give the user a useful hint about what went wrong, an error message is displayed at the "Status Message" field and a "Status Code" of "404" is filled manually.
(3) Unsuccessful or failed response
Nearly all the code examples I could find regarding usage of GlideAjax understate error handling, but a robust client application must be able to respond appropriately to errors as well.
Many developers do not know that the GlideAjax object also provides a way to register a callback function for the error case:
var gaRequest = new GlideAjax('<script>');
gaRequest.setErrorCallback(function (request) {
var intStatus = request.status;
var strStatusText = request.statusText;
var strResponseText = request.responseText;
});
With the help of such an error callback function, it is possible to handle the failed response accordingly:
Usage
After loading the provided update set (see next chapter) you will find the record producer "GlideAjax Test Client" at Service Catalog > Catalog Definitions > Record Producers. You can assign the record producer to any service catalog and catalog category you want. You also can configure user criteria to make the record producer only available to certain users.
To use the record producer you either can click on "Try It" in the UI16 or you open the respective service portal and navigate through the service catalog to open the record producer.
Source Code
The record producer and all of its dependent artifacts are bundled in an update set which can be downloaded from my respective Share Project at https://developer.servicenow.com/connect.do#!/share/contents/4067296_glideajax_test_client
- 2,781 Views