getXMLAnswer sends a request from a client script to a server-side method and passes the answer element of the response to a callback function. The call is asynchronous, so the page keeps responding while the server works. It belongs to the GlideAjax class, which needs a client callable script include on the server.
This page covers getXMLAnswer as part of the wider GlideAjax class, because the method is not used on its own. The request is built from a GlideAjax object, loaded with parameters, and then sent by one of the class’s calling methods. The sections below explain each piece, the server side that answers the request, and how getXMLAnswer compares with getXML and getXMLWait.
What the GlideAjax class does
GlideAjax allows the execution of server-side code from the client. A client script runs in the user’s browser, so it reaches server logic through this class. The process has three parts: the client script that builds the request, the server script include that holds the method, and the callback function in the client script that receives the result.
A mismatch in any one of them shows up as a request that returns nothing useful. Pass the name of the script include class to the GlideAjax constructor. The method named in sysparm_name must exist as a function on that script include, and the callback must be ready to receive what the server returns. For the client side of the pattern, see how client scripts work.
Building the request on the client
Start by creating the GlideAjax object. The constructor takes one argument, class_name, which is the name of the server-side class that contains the method you want to run. Pass the script include name exactly as it is written on the server.
Next, add parameters with addParam. Each call takes a parameter name and a value and passes them to the server-side function linked to the object. Every parameter name must begin with the prefix sysparm_. The first call to addParam should use the name sysparm_name, with the name of the server method as its value. Any further parameters, such as a record identifier or a user name, follow after that and also use the sysparm_ prefix.
addParam does not run the server code. The server-side code does not execute until the client script calls getXML or getXMLAnswer, so the usual order is to build the object, add every parameter, and then make the call. Adding a parameter late in the script is safe as long as it happens before that call.
Calling getXMLAnswer and handling the callback
getXMLAnswer takes up to three arguments. The first is the callback function. The second is an optional name-value pair of additional parameters. The third is an optional value that is passed as the second argument to the callback function, so the callback can receive context without relying on a global variable.
The method calls the server asynchronously and gets the answer element of the response in XML format. The callback receives that answer element as its argument. The method’s return type is void, so the answer reaches the script through the callback.
Because the call is asynchronous, code written after getXMLAnswer runs before the answer arrives. Any logic that depends on the answer belongs inside the callback function. A common pattern is to read the answer element in the callback and then write the value into a field on the form, or to show a message to the user once the value is known. Anything placed directly after the call, such as an attempt to read the field the answer should fill, will run too early. For the tools used to test scripts like this, see the script debugger.
The optional responseParam is passed to the callback as its second argument.
The server side script include
The server half of the request is a script include with three settings that have to be in place before a client call can reach it.
- The name field must be exactly the same as the class name used in the client script.
- The Client callable check box must be selected.
- The script include must extend AbstractAjaxProcessor and must be Glide AJAX enabled.
The method named in sysparm_name is a function on the script include. The sysparm_name parameter is the one GlideAjax uses to choose which function runs, so the function name you pass there has to match a function defined on the script include. Inside that function, read each incoming value with this.getParameter, passing the full parameter name, such as sysparm_record_id. The parameter name in the call must match the name used in getParameter exactly, including the sysparm_ prefix. For the wider concept behind the server half, see script includes.
Function names that begin with an underscore are private. They are not callable from the client, so the underscore is the right way to mark helper functions that only the server side uses.
Avoid using the following predefined names for your own custom parameters: sysparm_name, sysparm_function, sysparm_value and sysparm_type. Choose names such as sysparm_record_id or sysparm_user_name for your own values instead.
The script include should also leave the built-in AbstractAjaxProcessor methods alone. Avoid overriding these methods, including initialize. Put your own logic in new functions with their own names.
Choosing between getXMLAnswer, getXML and getXMLWait
The class has three ways to send the request. They differ in how they wait for the server and in what the callback or caller receives.
| Method | How it runs | What the callback or caller receives | Notes |
|---|---|---|---|
| getXML(callback) | Asynchronous | The results the server returns, delivered through the callback when ready | Preferred method for executing server code |
| getXMLAnswer(callback, additionalParam, responseParam) | Asynchronous | The answer element of the response, in XML format | Accepts optional extra parameters and an optional second callback argument |
| getXMLWait() | Synchronous | Nothing is returned directly; the client calls getAnswer() to retrieve the result | Available but not recommended; the server does not handle further client requests until it finishes, and it is not available to scoped applications |
The practical difference between getXML and getXMLAnswer is the shape of what the callback gets. With getXML, the callback receives the results the server returns. With getXMLAnswer, the callback receives only the answer element, in XML format, which removes one step from the callback. The method also adds the two optional arguments described above.
getXMLWait is the one to treat with care. It is synchronous, so the client waits for the server before the next line of code runs. The cost is that the server does not handle further client requests until it finishes. For how server-side work affects the platform more broadly, see platform performance. The method is not available to scoped applications, so a scoped application has to use getXML or getXMLAnswer. getAnswer retrieves the result of the server method called through getXMLWait.
Common mistakes
The common failure here is a parameter named without the sysparm_ prefix, which is required. Check the prefix on every addParam call first.
The second common failure is an addParam sequence that does not begin with sysparm_name. The first call is the one that names the server method, so a custom parameter placed first leaves the request without a clear target.
The third is a mismatch between the class name and the script include name. The two have to be identical, including capitalisation.
The fourth is a missing Client callable setting. Without it, the script include is not reachable from the client, so the request does not return the answer the callback is waiting for.
The fifth is reading the answer straight after the call. Because the call is asynchronous, the code that uses the result belongs in the callback, and nothing placed after the call can depend on the answer.
The sixth is calling a helper function that begins with an underscore from the client. Those functions are private by design, so the call will not reach them. Move any helper the client needs to a public method, and keep the underscore for helpers that only the server uses.
The seventh is choosing getXMLWait out of habit because its order looks simpler. The synchronous wait holds the server for the next request, and a scoped application cannot use it at all. getXML is the preferred method because it is asynchronous, so the callback pattern with getXML is the one to start from.
Frequently asked questions
Why is there no result straight after I call getXMLAnswer?
The call is asynchronous, so the callback runs only when the server has responded. Any code that needs the answer should be inside the callback function, not directly after the call.
Does adding a parameter run the server code?
No. The server-side code does not execute until getXML or getXMLAnswer is called. Each addParam call only passes its name and value to the GlideAjax object, to be sent with the request.
What is the difference between getXMLAnswer and getXML?
Both are asynchronous. The getXML callback receives the results the server returns. The getXMLAnswer callback receives the answer element of the response directly, in XML format, and the method also accepts the two optional arguments.
Which parameter has to come first?
The first call to addParam should use sysparm_name, with the name of the server method as its value. Every later parameter also uses the sysparm_ prefix.
Can I use getXMLWait in a scoped application?
No. getXMLWait is not available to scoped applications. Use getXML instead, which is the preferred method, or getXMLAnswer if you want the answer element directly.
Which names should I avoid for custom parameters?
Avoid sysparm_name, sysparm_function, sysparm_value and sysparm_type, because these are predefined names in the GlideAjax mechanism. Use your own sysparm_ names for everything else.