GlideAjax - Product Documentation: Utah (2024)

\n

\n\n \n \n

The GlideAjax class enables a client script to call server-side code\n in a script include.

\n\n

To use GlideAjax in a client script, follow these general steps.

\n\n

  1. Create a GlideAjax instance by calling the GlideAjax\n constructor. As the argument to the constructor, specify the name of the script include\n class that contains the method you want to call.
  2. Call the addParam method with the sysparm_name\n parameter and the name of the script-include method you want to call.
  3. (Optional) Call the addParam method one or more times to provide the\n script-include code with other parameters it needs.
  4. Execute the server-side code by calling getXML().\n

    Note: getXML() is the preferred method for executing the code,\n because it is asynchronous and does not hold up the execution of other client code.\n Another method, getXMLWait(), is also available but is not recommended.\n Using getXMLWait() ensures the order of execution, but can cause the\n application to seem unresponsive, significantly degrading the user experience of any\n application that uses it. getXMLWait() is not available to scoped\n applications.

    \n

\n\n

\n

var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class \nga.addParam('sysparm_name','helloWorld'); // helloWorld is the script include method \nga.addParam('sysparm_user_name',"Bob"); // Set parameter sysparm_user_name to 'Bob' \nga.getXML(HelloWorldParse); /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' \n and use the callback function HelloWorldParse() to return the result when ready */\n\n// the callback function for returning the result from the server-side code\nfunction HelloWorldParse(response) { \n var answer = response.responseXML.documentElement.getAttribute("answer"); \n alert(answer);\n}

\n

\n\n

\n\n

\n

\n

Parent Topic: Client API reference

\n

\n

\n

GlideAjax - GlideAjax(Stringclass_name)

\n\n \n \n

Constructor for GlideAjax.

\n\n

\n \n

Table 1. Parameters
NameTypeDescription
class_nameStringThe name of the server-side class that contains the method you want to execute.

\n

\n\n

\n\n

\n

In this example, a client script sets the user to Fred Luddy and then calls a script\n include to get their manager.

\n\n

// client script – contains onLoad function and a callback function\n\nfunction onLoad() {\n var ga = new GlideAjax('GetUserInfo'); // GetUserInfo is the script include name \n ga.addParam('sysparm_name','managerName'); // managerName is the function in the script include that we're calling \n ga.addParam('sysparm_user_name','fred.luddy'); // set user to Fred Luddy \n\n /* Call GetUserInfo.managerName() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */\n ga.getXMLAnswer(ManagerParse); \t\t\n}\n\n// callback function for returning the result from the script include\nfunction ManagerParse(response) { \n alert(response);\n}\n\n\n// GetUserInfo script include \n\nvar GetUserInfo = Class.create();\nGetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {\n\n managerName: function() {\n var userName = this.getParameter("sysparm_user_name");\n var grUser = new GlideRecord('sys_user');\n grUser.get("user_name", userName);\n\n // Build the payload. You can return additional data if needed. \n var result = { \n "manager": grUser.getDisplayValue('manager')\n }; \n return JSON.stringify(result);\n },\n type: 'GetUserInfo'\n});\n

\n

\n\n

\n\n

\n

\n

GlideAjax - addParam(Stringparm_name, Stringparm_value)

\n\n \n \n

Specifies a parameter name and value to be passed to the server-side function associated\n with this GlideAjax object.

\n\n

\n

You can execute addParam multiple times with different parameters and values.\n

Note: The first call to addParam should be with the parameter\n sysparm_name and the name of the server-side method you want to\n call. The server-side code does not execute until the client script calls\n getXML() or getXMLAnswer().

\n

\n\n \n

Table 2. Parameters
NameTypeDescription
parm_nameStringThe name of the parameter to pass. (The name must begin with the\n sysparm_ .)
parm_valueStringThe value to assign to parm_name.

\n

\n\n \n

Table 3. Returns
TypeDescription
void

\n

\n\n

\n\n

\n

In this example, a client script sets the user to Fred Luddy and then calls a script\n include to get their manager.

\n\n

// client script – contains onLoad function and a callback function\n\nfunction onLoad() {\n var ga = new GlideAjax('GetUserInfo'); // GetUserInfo is the script include name \n ga.addParam('sysparm_name','managerName'); // managerName is the function in the script include that we're calling \n ga.addParam('sysparm_user_name','fred.luddy'); // set user to Fred Luddy \n\n /* Call GetUserInfo.managerName() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */\n ga.getXMLAnswer(ManagerParse); \t\t\n}\n\n// callback function for returning the result from the script include\nfunction ManagerParse(response) { \n alert(response);\n}\n\n\n// GetUserInfo script include \n\nvar GetUserInfo = Class.create();\nGetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {\n\n managerName: function() {\n var userName = this.getParameter("sysparm_user_name");\n var grUser = new GlideRecord('sys_user');\n grUser.get("user_name", userName);\n\n // Build the payload. You can return additional data if needed. \n var result = { \n "manager": grUser.getDisplayValue('manager')\n }; \n return JSON.stringify(result);\n },\n type: 'GetUserInfo'\n});\n

\n

\n\n

\n\n

\n

\n

GlideAjax - getAnswer()

\n\n \n \n

Retrieves the results from a server-side method called from the client via\n getXMLWait().

\n\n

\n \n

Table 4. Parameters
NameTypeDescription
none

\n

\n\n \n

Table 5. Returns
TypeDescription
voidThe result returned by the server-side method previously called with\n getXMLWait().

\n

\n\n

\n\n

\n\n

\n

\n

GlideAjax - getXMLAnswer(Function callback, Object additionalParam, Object\n responseParam)

\n\n \n \n

Calls the processor asynchronously and gets the answer element of the response in XML\n format.

\n\n

\n \n

Table 6. Parameters
NameTypeDescription
callbackFunctionCallback function. The function receives the answer element of the response in\n XML format as an argument.
additionalParamObjectOptional. Name-value pair of additional parameters.
responseParamObjectOptional. Second argument for the callback function.

\n

\n\n \n

Table 7. Returns
TypeDescription
void

\n

\n\n

\n\n

\n

Returns the number of attachments

\n\n

function updateAttachmentCount(sysid) {\n var ga = new GlideAjax('AttachmentAjax');\n ga.addParam('sysparm_type', 'attachmentCount');\n ga.addParam('sysparm_value', sysid);\n ga.getXMLAnswer(numberOfAttachments, null, sysid); // callback: numberOfAttachments with args (answer, sysid)\n}\n\nfunction numberOfAttachments(answer, sysid) {\n // we want to know there are 5 attachments, not 5.0 attachments\n var number = parseInt(answer);\n var buttons = $$('.attachmentNumber_' + sysid);\n if (buttons[0] == undefined)\n $('header_attachment_list_label').down().innerHTML = number;\n else {\n for (var i = 0; i < buttons.length; i++) {\n buttons[i].innerHTML = number;\n }\n }\n}

\n

\n\n

\n\n

\n

\n

GlideAjax - getXML(Functioncallback)

\n\n \n \n

Sends the server a request to execute the method and parameters associated with this\n GlideAjax object.

\n\n

\n

The server processes the request asynchronously and -- when ready -- returns the results via the\n function specified as the callback_function.

\n\n \n

Table 8. Parameters
NameTypeDescription
callbackFunctionThe name of the callback function to process the results returned by the server.

\n

\n\n \n

Table 9. Returns
TypeDescription
void

\n

\n\n

\n\n

\n

var comments = gel("dialog_comments").value;\nvar ga = new GlideAjax('validateComments'); //Call script include to escape text\nga.addParam('sysparm_name', 'validateComments');\nga.addParam('sysparm_comments', comments);\nga.getXML(callback);\n \nreturn false;\n \nfunction callback(response) {\n var comments = response.responseXML.documentElement.getAttribute("answer");\n comments = trim(comments);\n if (comments == "") {\n //If comments are empty, alert the user and stop submission\n alert("Please enter your comments before submitting.");\n } else {\n //If there are comments, close the dialog window and submit them\n GlideDialogWindow.get().destroy(); //Close the dialog window\n g_form.setValue("comments", comments); //Set the "Comments" field with comments in the dialog\n }

\n

\n\n

\n\n

\n

\n

GlideAjax - getXMLWait()

\n\n \n \n

Sends the server a request to execute the method and parameters associated with this\n GlideAjax object.

\n\n

\n

The server processes the request synchronously and will not process further requests from the\n client until finished. To retrieve the results, the client must call\n getAnswer(). Using getXMLWait() ensures the order of\n execution, but can cause the application to seem unresponsive, significantly degrading the\n user experience of any application that uses it. We recommend using\n getXML() instead.

Note: getXMLWait() is not available to\n scoped applications.

\n

\n\n \n

Table 10. Parameters
NameTypeDescription
none

\n

\n\n \n

Table 11. Returns
TypeDescription
void

\n

\n\n

\n\n

\n

var ga = new GlideAjax('HelloWorld');\n ga.addParam('sysparm_name','helloWorld');\n ga.addParam('sysparm_user_name',"Bob");\n ga.getXMLWait();\n alert(ga.getAnswer());

\n

\n\n

Scoped equivalent

\n \n

getXMLWait() is not available to scoped applications. Instead use the\n \n method.

\n\n

\n\n

\n\n

\n

GlideAjax  - Product Documentation: Utah (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jerrold Considine

Last Updated:

Views: 5752

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.