[{"data":1,"prerenderedAt":1674},["ShallowReactive",2],{"page-\u002Fblog\u002Fservicenow-glideajax-guide\u002F":3,"related-\u002Fblog\u002Fservicenow-glideajax-guide\u002F":1642},{"id":4,"title":5,"author":6,"authorUrl":7,"body":8,"date":1614,"dateUpdated":1614,"description":1615,"dqid":1614,"excerpt":1616,"extension":1617,"faq":1618,"headline":5,"meta":1631,"navigation":289,"path":1632,"seo":1633,"socialImage":1634,"stem":1635,"tags":1636,"__hash__":1641},"content\u002Fblog\u002Fservicenow-glideajax-guide.md","ServiceNow GlideAjax: Call a Script Include from a Client Script","Gabriel Fordan","https:\u002F\u002Fsn-tricks.com\u002Fabout",{"type":9,"value":10,"toc":1603},"minimark",[11,36,41,49,62,199,203,221,224,681,684,724,731,782,786,793,1166,1169,1190,1208,1212,1220,1348,1356,1360,1369,1372,1376,1379,1382,1426,1438,1442,1445,1511,1515,1518,1540,1544,1587,1599],[12,13,14,15,19,20,23,24,27,28,31,32,35],"p",{},"GlideAjax is how a ServiceNow client script calls a Script Include on the server without freezing the form. You write a client-callable Script Include that extends ",[16,17,18],"code",{},"AbstractAjaxProcessor",", then in the client script you create a ",[16,21,22],{},"GlideAjax"," object with the Script Include name, pass the method name in ",[16,25,26],{},"sysparm_name",", add your own ",[16,29,30],{},"sysparm_"," parameters, and read the result in a ",[16,33,34],{},"getXMLAnswer()"," callback. That is the whole pattern. The rest of this guide builds a working example and covers the parts that cause most bugs: return values, scope names and security.",[37,38,40],"h2",{"id":39},"when-to-use-glideajax","When to use GlideAjax",[12,42,43,44,48],{},"Use GlideAjax when the browser needs server data ",[45,46,47],"strong",{},"after"," the form has loaded, usually in an onChange client script. Typical cases are looking up a user's department when the caller changes, checking whether a CI is in a maintenance window, or validating a value against a table the user cannot see on the form.",[12,50,51,52,55,56,61],{},"If the data is needed when the form opens, a display business rule that fills ",[16,53,54],{},"g_scratchpad"," is cheaper because it rides along with the form load. The ",[57,58,60],"a",{"href":59},"\u002Fblog\u002Fservicenow-client-script-best-practices","client script best practices guide"," covers that choice in more detail.",[63,64,65,87],"table",{},[66,67,68],"thead",{},[69,70,71,75,78,81,84],"tr",{},[72,73,74],"th",{},"Option",[72,76,77],{},"When it runs",[72,79,80],{},"Blocks the form",[72,82,83],{},"Works in Service Portal",[72,85,86],{},"Good for",[88,89,90,110,129,147,164,182],"tbody",{},[69,91,92,98,101,104,107],{},[93,94,95,97],"td",{},[16,96,54],{}," from a display business rule",[93,99,100],{},"Form load",[93,102,103],{},"No",[93,105,106],{},"Yes",[93,108,109],{},"Data needed on load",[69,111,112,119,122,124,126],{},[93,113,114,116,117],{},[16,115,22],{}," with ",[16,118,34],{},[93,120,121],{},"On demand",[93,123,103],{},[93,125,106],{},[93,127,128],{},"Data needed after a change",[69,130,131,138,140,142,144],{},[93,132,133,116,135],{},[16,134,22],{},[16,136,137],{},"getXML()",[93,139,121],{},[93,141,103],{},[93,143,106],{},[93,145,146],{},"Same, when you want the full response",[69,148,149,155,157,159,161],{},[93,150,151,154],{},[16,152,153],{},"g_form.getReference()"," with a callback",[93,156,121],{},[93,158,103],{},[93,160,106],{},[93,162,163],{},"One field from one referenced record",[69,165,166,173,175,177,179],{},[93,167,168,116,170],{},[16,169,22],{},[16,171,172],{},"getXMLWait()",[93,174,121],{},[93,176,106],{},[93,178,103],{},[93,180,181],{},"Avoid",[69,183,184,190,192,195,197],{},[93,185,186,187],{},"Client-side ",[16,188,189],{},"GlideRecord",[93,191,121],{},[93,193,194],{},"Usually",[93,196,103],{},[93,198,181],{},[37,200,202],{"id":201},"step-1-create-the-client-callable-script-include","Step 1: Create the client-callable Script Include",[12,204,205,206,209,210,213,214,216,217,220],{},"Go to ",[45,207,208],{},"System Definition > Script Includes"," and create a new record. Select ",[45,211,212],{},"Client callable",". The class must extend ",[16,215,18],{},", which provides ",[16,218,219],{},"getParameter()"," and handles the request plumbing.",[12,222,223],{},"This example returns a caller's VIP flag, department and manager as JSON:",[225,226,231],"pre",{"className":227,"code":228,"language":229,"meta":230,"style":230},"language-javascript shiki shiki-themes github-light github-light","var CallerInfoAjax = Class.create();\nCallerInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {\n\n  getCallerInfo: function() {\n    var userId = this.getParameter('sysparm_user_id');\n\n    \u002F\u002F Reject anything that is not a sys_id before touching the database\n    if (!userId || !\u002F^[0-9a-f]{32}$\u002F.test(userId)) {\n      return JSON.stringify({ ok: false, error: 'invalid_user' });\n    }\n\n    \u002F\u002F Only users who can work incidents need this data\n    if (!gs.hasRole('itil')) {\n      return JSON.stringify({ ok: false, error: 'not_allowed' });\n    }\n\n    \u002F\u002F GlideRecordSecure applies ACLs for the calling user\n    var user = new GlideRecordSecure('sys_user');\n    if (!user.get(userId)) {\n      return JSON.stringify({ ok: false, error: 'not_found' });\n    }\n\n    return JSON.stringify({\n      ok: true,\n      vip: String(user.vip) == 'true',\n      department: user.getDisplayValue('department'),\n      manager: user.getDisplayValue('manager')\n    });\n  },\n\n  type: 'CallerInfoAjax'\n});\n","javascript","",[16,232,233,259,284,291,306,335,340,347,390,419,425,430,436,459,481,486,491,497,520,537,559,564,569,584,596,616,633,649,655,661,666,675],{"__ignoreMap":230},[234,235,238,242,246,249,252,256],"span",{"class":236,"line":237},"line",1,[234,239,241],{"class":240},"sCydW","var",[234,243,245],{"class":244},"sKWpL"," CallerInfoAjax ",[234,247,248],{"class":240},"=",[234,250,251],{"class":244}," Class.",[234,253,255],{"class":254},"se37E","create",[234,257,258],{"class":244},"();\n",[234,260,262,266,269,272,275,278,281],{"class":236,"line":261},2,[234,263,265],{"class":264},"sMN4m","CallerInfoAjax",[234,267,268],{"class":244},".",[234,270,271],{"class":264},"prototype",[234,273,274],{"class":240}," =",[234,276,277],{"class":244}," Object.",[234,279,280],{"class":254},"extendsObject",[234,282,283],{"class":244},"(AbstractAjaxProcessor, {\n",[234,285,287],{"class":236,"line":286},3,[234,288,290],{"emptyLinePlaceholder":289},true,"\n",[234,292,294,297,300,303],{"class":236,"line":293},4,[234,295,296],{"class":254},"  getCallerInfo",[234,298,299],{"class":244},": ",[234,301,302],{"class":240},"function",[234,304,305],{"class":244},"() {\n",[234,307,309,312,315,317,320,322,325,328,332],{"class":236,"line":308},5,[234,310,311],{"class":240},"    var",[234,313,314],{"class":244}," userId ",[234,316,248],{"class":240},[234,318,319],{"class":264}," this",[234,321,268],{"class":244},[234,323,324],{"class":254},"getParameter",[234,326,327],{"class":244},"(",[234,329,331],{"class":330},"sOTlB","'sysparm_user_id'",[234,333,334],{"class":244},");\n",[234,336,338],{"class":236,"line":337},6,[234,339,290],{"emptyLinePlaceholder":289},[234,341,343],{"class":236,"line":342},7,[234,344,346],{"class":345},"sJ8bj","    \u002F\u002F Reject anything that is not a sys_id before touching the database\n",[234,348,350,353,356,359,362,365,368,371,374,377,380,382,384,387],{"class":236,"line":349},8,[234,351,352],{"class":240},"    if",[234,354,355],{"class":244}," (",[234,357,358],{"class":240},"!",[234,360,361],{"class":244},"userId ",[234,363,364],{"class":240},"||",[234,366,367],{"class":240}," !",[234,369,370],{"class":330},"\u002F",[234,372,373],{"class":240},"^",[234,375,376],{"class":264},"[0-9a-f]",[234,378,379],{"class":240},"{32}$",[234,381,370],{"class":330},[234,383,268],{"class":244},[234,385,386],{"class":254},"test",[234,388,389],{"class":244},"(userId)) {\n",[234,391,393,396,399,401,404,407,410,413,416],{"class":236,"line":392},9,[234,394,395],{"class":240},"      return",[234,397,398],{"class":264}," JSON",[234,400,268],{"class":244},[234,402,403],{"class":254},"stringify",[234,405,406],{"class":244},"({ ok: ",[234,408,409],{"class":264},"false",[234,411,412],{"class":244},", error: ",[234,414,415],{"class":330},"'invalid_user'",[234,417,418],{"class":244}," });\n",[234,420,422],{"class":236,"line":421},10,[234,423,424],{"class":244},"    }\n",[234,426,428],{"class":236,"line":427},11,[234,429,290],{"emptyLinePlaceholder":289},[234,431,433],{"class":236,"line":432},12,[234,434,435],{"class":345},"    \u002F\u002F Only users who can work incidents need this data\n",[234,437,439,441,443,445,448,451,453,456],{"class":236,"line":438},13,[234,440,352],{"class":240},[234,442,355],{"class":244},[234,444,358],{"class":240},[234,446,447],{"class":244},"gs.",[234,449,450],{"class":254},"hasRole",[234,452,327],{"class":244},[234,454,455],{"class":330},"'itil'",[234,457,458],{"class":244},")) {\n",[234,460,462,464,466,468,470,472,474,476,479],{"class":236,"line":461},14,[234,463,395],{"class":240},[234,465,398],{"class":264},[234,467,268],{"class":244},[234,469,403],{"class":254},[234,471,406],{"class":244},[234,473,409],{"class":264},[234,475,412],{"class":244},[234,477,478],{"class":330},"'not_allowed'",[234,480,418],{"class":244},[234,482,484],{"class":236,"line":483},15,[234,485,424],{"class":244},[234,487,489],{"class":236,"line":488},16,[234,490,290],{"emptyLinePlaceholder":289},[234,492,494],{"class":236,"line":493},17,[234,495,496],{"class":345},"    \u002F\u002F GlideRecordSecure applies ACLs for the calling user\n",[234,498,500,502,505,507,510,513,515,518],{"class":236,"line":499},18,[234,501,311],{"class":240},[234,503,504],{"class":244}," user ",[234,506,248],{"class":240},[234,508,509],{"class":240}," new",[234,511,512],{"class":254}," GlideRecordSecure",[234,514,327],{"class":244},[234,516,517],{"class":330},"'sys_user'",[234,519,334],{"class":244},[234,521,523,525,527,529,532,535],{"class":236,"line":522},19,[234,524,352],{"class":240},[234,526,355],{"class":244},[234,528,358],{"class":240},[234,530,531],{"class":244},"user.",[234,533,534],{"class":254},"get",[234,536,389],{"class":244},[234,538,540,542,544,546,548,550,552,554,557],{"class":236,"line":539},20,[234,541,395],{"class":240},[234,543,398],{"class":264},[234,545,268],{"class":244},[234,547,403],{"class":254},[234,549,406],{"class":244},[234,551,409],{"class":264},[234,553,412],{"class":244},[234,555,556],{"class":330},"'not_found'",[234,558,418],{"class":244},[234,560,562],{"class":236,"line":561},21,[234,563,424],{"class":244},[234,565,567],{"class":236,"line":566},22,[234,568,290],{"emptyLinePlaceholder":289},[234,570,572,575,577,579,581],{"class":236,"line":571},23,[234,573,574],{"class":240},"    return",[234,576,398],{"class":264},[234,578,268],{"class":244},[234,580,403],{"class":254},[234,582,583],{"class":244},"({\n",[234,585,587,590,593],{"class":236,"line":586},24,[234,588,589],{"class":244},"      ok: ",[234,591,592],{"class":264},"true",[234,594,595],{"class":244},",\n",[234,597,599,602,605,608,611,614],{"class":236,"line":598},25,[234,600,601],{"class":244},"      vip: ",[234,603,604],{"class":254},"String",[234,606,607],{"class":244},"(user.vip) ",[234,609,610],{"class":240},"==",[234,612,613],{"class":330}," 'true'",[234,615,595],{"class":244},[234,617,619,622,625,627,630],{"class":236,"line":618},26,[234,620,621],{"class":244},"      department: user.",[234,623,624],{"class":254},"getDisplayValue",[234,626,327],{"class":244},[234,628,629],{"class":330},"'department'",[234,631,632],{"class":244},"),\n",[234,634,636,639,641,643,646],{"class":236,"line":635},27,[234,637,638],{"class":244},"      manager: user.",[234,640,624],{"class":254},[234,642,327],{"class":244},[234,644,645],{"class":330},"'manager'",[234,647,648],{"class":244},")\n",[234,650,652],{"class":236,"line":651},28,[234,653,654],{"class":244},"    });\n",[234,656,658],{"class":236,"line":657},29,[234,659,660],{"class":244},"  },\n",[234,662,664],{"class":236,"line":663},30,[234,665,290],{"emptyLinePlaceholder":289},[234,667,669,672],{"class":236,"line":668},31,[234,670,671],{"class":244},"  type: ",[234,673,674],{"class":330},"'CallerInfoAjax'\n",[234,676,678],{"class":236,"line":677},32,[234,679,680],{"class":244},"});\n",[12,682,683],{},"A few rules this example follows:",[685,686,687,702,713],"ul",{},[688,689,690,693,694,697,698,701],"li",{},[45,691,692],{},"Return a string."," The return value becomes the ",[16,695,696],{},"answer"," the client receives. Build an object and pass it through ",[16,699,700],{},"JSON.stringify()"," when you need more than one value.",[688,703,704,710,711,268],{},[45,705,706,707,268],{},"Read input with ",[16,708,709],{},"this.getParameter()"," Custom parameter names must start with ",[16,712,30],{},[688,714,715,718,719,723],{},[45,716,717],{},"Keep the method small."," It runs on every call, so query only what you need and avoid loops over large tables. The ",[57,720,722],{"href":721},"\u002Fblog\u002Fservicenow-gliderecord-query-performance","GlideRecord query performance guide"," applies here too.",[12,725,726,727,730],{},"In a scoped application, extend ",[16,728,729],{},"global.AbstractAjaxProcessor"," instead:",[225,732,734],{"className":227,"code":733,"language":229,"meta":230,"style":230},"var CallerInfoAjax = Class.create();\nCallerInfoAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {\n  \u002F\u002F methods as above\n  type: 'CallerInfoAjax'\n});\n",[16,735,736,750,767,772,778],{"__ignoreMap":230},[234,737,738,740,742,744,746,748],{"class":236,"line":237},[234,739,241],{"class":240},[234,741,245],{"class":244},[234,743,248],{"class":240},[234,745,251],{"class":244},[234,747,255],{"class":254},[234,749,258],{"class":244},[234,751,752,754,756,758,760,762,764],{"class":236,"line":261},[234,753,265],{"class":264},[234,755,268],{"class":244},[234,757,271],{"class":264},[234,759,274],{"class":240},[234,761,277],{"class":244},[234,763,280],{"class":254},[234,765,766],{"class":244},"(global.AbstractAjaxProcessor, {\n",[234,768,769],{"class":236,"line":286},[234,770,771],{"class":345},"  \u002F\u002F methods as above\n",[234,773,774,776],{"class":236,"line":293},[234,775,671],{"class":244},[234,777,674],{"class":330},[234,779,780],{"class":236,"line":308},[234,781,680],{"class":244},[37,783,785],{"id":784},"step-2-call-it-from-the-client-script","Step 2: Call it from the client script",[12,787,788,789,792],{},"Create an onChange client script on the Incident table for the ",[45,790,791],{},"Caller"," field:",[225,794,796],{"className":227,"code":795,"language":229,"meta":230,"style":230},"function onChange(control, oldValue, newValue, isLoading, isTemplate) {\n  if (isLoading || newValue === '') {\n    g_form.hideFieldMsg('caller_id', true);\n    return;\n  }\n\n  var ga = new GlideAjax('CallerInfoAjax');\n  ga.addParam('sysparm_name', 'getCallerInfo');\n  ga.addParam('sysparm_user_id', newValue);\n  ga.getXMLAnswer(function(answer) {\n    var result;\n    try {\n      result = JSON.parse(answer);\n    } catch (e) {\n      return; \u002F\u002F Empty or unexpected response; leave the form as it is\n    }\n\n    g_form.hideFieldMsg('caller_id', true);\n    if (!result.ok) {\n      return;\n    }\n\n    if (result.vip) {\n      g_form.showFieldMsg('caller_id', 'VIP caller', 'info');\n    }\n    if (result.department) {\n      g_form.addInfoMessage('Department: ' + result.department +\n        (result.manager ? ', manager: ' + result.manager : ''));\n    }\n  });\n}\n",[16,797,798,835,856,875,882,887,891,913,933,946,963,970,978,995,1006,1016,1020,1024,1040,1051,1057,1061,1065,1072,1096,1100,1107,1128,1152,1156,1161],{"__ignoreMap":230},[234,799,800,802,805,807,811,814,817,819,822,824,827,829,832],{"class":236,"line":237},[234,801,302],{"class":240},[234,803,804],{"class":254}," onChange",[234,806,327],{"class":244},[234,808,810],{"class":809},"sj_tP","control",[234,812,813],{"class":244},", ",[234,815,816],{"class":809},"oldValue",[234,818,813],{"class":244},[234,820,821],{"class":809},"newValue",[234,823,813],{"class":244},[234,825,826],{"class":809},"isLoading",[234,828,813],{"class":244},[234,830,831],{"class":809},"isTemplate",[234,833,834],{"class":244},") {\n",[234,836,837,840,843,845,848,851,854],{"class":236,"line":261},[234,838,839],{"class":240},"  if",[234,841,842],{"class":244}," (isLoading ",[234,844,364],{"class":240},[234,846,847],{"class":244}," newValue ",[234,849,850],{"class":240},"===",[234,852,853],{"class":330}," ''",[234,855,834],{"class":244},[234,857,858,861,864,866,869,871,873],{"class":236,"line":286},[234,859,860],{"class":244},"    g_form.",[234,862,863],{"class":254},"hideFieldMsg",[234,865,327],{"class":244},[234,867,868],{"class":330},"'caller_id'",[234,870,813],{"class":244},[234,872,592],{"class":264},[234,874,334],{"class":244},[234,876,877,879],{"class":236,"line":293},[234,878,574],{"class":240},[234,880,881],{"class":244},";\n",[234,883,884],{"class":236,"line":308},[234,885,886],{"class":244},"  }\n",[234,888,889],{"class":236,"line":337},[234,890,290],{"emptyLinePlaceholder":289},[234,892,893,896,899,901,903,906,908,911],{"class":236,"line":342},[234,894,895],{"class":240},"  var",[234,897,898],{"class":244}," ga ",[234,900,248],{"class":240},[234,902,509],{"class":240},[234,904,905],{"class":254}," GlideAjax",[234,907,327],{"class":244},[234,909,910],{"class":330},"'CallerInfoAjax'",[234,912,334],{"class":244},[234,914,915,918,921,923,926,928,931],{"class":236,"line":349},[234,916,917],{"class":244},"  ga.",[234,919,920],{"class":254},"addParam",[234,922,327],{"class":244},[234,924,925],{"class":330},"'sysparm_name'",[234,927,813],{"class":244},[234,929,930],{"class":330},"'getCallerInfo'",[234,932,334],{"class":244},[234,934,935,937,939,941,943],{"class":236,"line":392},[234,936,917],{"class":244},[234,938,920],{"class":254},[234,940,327],{"class":244},[234,942,331],{"class":330},[234,944,945],{"class":244},", newValue);\n",[234,947,948,950,953,955,957,959,961],{"class":236,"line":421},[234,949,917],{"class":244},[234,951,952],{"class":254},"getXMLAnswer",[234,954,327],{"class":244},[234,956,302],{"class":240},[234,958,327],{"class":244},[234,960,696],{"class":809},[234,962,834],{"class":244},[234,964,965,967],{"class":236,"line":427},[234,966,311],{"class":240},[234,968,969],{"class":244}," result;\n",[234,971,972,975],{"class":236,"line":432},[234,973,974],{"class":240},"    try",[234,976,977],{"class":244}," {\n",[234,979,980,983,985,987,989,992],{"class":236,"line":438},[234,981,982],{"class":244},"      result ",[234,984,248],{"class":240},[234,986,398],{"class":264},[234,988,268],{"class":244},[234,990,991],{"class":254},"parse",[234,993,994],{"class":244},"(answer);\n",[234,996,997,1000,1003],{"class":236,"line":461},[234,998,999],{"class":244},"    } ",[234,1001,1002],{"class":240},"catch",[234,1004,1005],{"class":244}," (e) {\n",[234,1007,1008,1010,1013],{"class":236,"line":483},[234,1009,395],{"class":240},[234,1011,1012],{"class":244},"; ",[234,1014,1015],{"class":345},"\u002F\u002F Empty or unexpected response; leave the form as it is\n",[234,1017,1018],{"class":236,"line":488},[234,1019,424],{"class":244},[234,1021,1022],{"class":236,"line":493},[234,1023,290],{"emptyLinePlaceholder":289},[234,1025,1026,1028,1030,1032,1034,1036,1038],{"class":236,"line":499},[234,1027,860],{"class":244},[234,1029,863],{"class":254},[234,1031,327],{"class":244},[234,1033,868],{"class":330},[234,1035,813],{"class":244},[234,1037,592],{"class":264},[234,1039,334],{"class":244},[234,1041,1042,1044,1046,1048],{"class":236,"line":522},[234,1043,352],{"class":240},[234,1045,355],{"class":244},[234,1047,358],{"class":240},[234,1049,1050],{"class":244},"result.ok) {\n",[234,1052,1053,1055],{"class":236,"line":539},[234,1054,395],{"class":240},[234,1056,881],{"class":244},[234,1058,1059],{"class":236,"line":561},[234,1060,424],{"class":244},[234,1062,1063],{"class":236,"line":566},[234,1064,290],{"emptyLinePlaceholder":289},[234,1066,1067,1069],{"class":236,"line":571},[234,1068,352],{"class":240},[234,1070,1071],{"class":244}," (result.vip) {\n",[234,1073,1074,1077,1080,1082,1084,1086,1089,1091,1094],{"class":236,"line":586},[234,1075,1076],{"class":244},"      g_form.",[234,1078,1079],{"class":254},"showFieldMsg",[234,1081,327],{"class":244},[234,1083,868],{"class":330},[234,1085,813],{"class":244},[234,1087,1088],{"class":330},"'VIP caller'",[234,1090,813],{"class":244},[234,1092,1093],{"class":330},"'info'",[234,1095,334],{"class":244},[234,1097,1098],{"class":236,"line":598},[234,1099,424],{"class":244},[234,1101,1102,1104],{"class":236,"line":618},[234,1103,352],{"class":240},[234,1105,1106],{"class":244}," (result.department) {\n",[234,1108,1109,1111,1114,1116,1119,1122,1125],{"class":236,"line":635},[234,1110,1076],{"class":244},[234,1112,1113],{"class":254},"addInfoMessage",[234,1115,327],{"class":244},[234,1117,1118],{"class":330},"'Department: '",[234,1120,1121],{"class":240}," +",[234,1123,1124],{"class":244}," result.department ",[234,1126,1127],{"class":240},"+\n",[234,1129,1130,1133,1136,1139,1141,1144,1147,1149],{"class":236,"line":651},[234,1131,1132],{"class":244},"        (result.manager ",[234,1134,1135],{"class":240},"?",[234,1137,1138],{"class":330}," ', manager: '",[234,1140,1121],{"class":240},[234,1142,1143],{"class":244}," result.manager ",[234,1145,1146],{"class":240},":",[234,1148,853],{"class":330},[234,1150,1151],{"class":244},"));\n",[234,1153,1154],{"class":236,"line":657},[234,1155,424],{"class":244},[234,1157,1158],{"class":236,"line":663},[234,1159,1160],{"class":244},"  });\n",[234,1162,1163],{"class":236,"line":668},[234,1164,1165],{"class":244},"}\n",[12,1167,1168],{},"What each line does:",[685,1170,1171,1180,1185],{},[688,1172,1173,1176,1177,268],{},[16,1174,1175],{},"new GlideAjax('CallerInfoAjax')"," names the Script Include. If the Script Include is in a scoped application, use its full API name, for example ",[16,1178,1179],{},"new GlideAjax('x_acme_helpdesk.CallerInfoAjax')",[688,1181,1182,1184],{},[16,1183,26],{}," is the method to run. It must match the method name exactly, including case.",[688,1186,1187,1189],{},[16,1188,34],{}," sends the request and calls your function with the answer string. Code after this line runs immediately, before the answer arrives.",[12,1191,1192,1193,1197,1198,1202,1203,1207],{},"The library has a ready ",[57,1194,1196],{"href":1195},"\u002Fscripts\u002Fglideajax-server-call","GlideAjax server call",", a ",[57,1199,1201],{"href":1200},"\u002Fscripts\u002Fglideajax-response-handler","response handler Script Include"," and a ",[57,1204,1206],{"href":1205},"\u002Fscripts\u002Fglideajax-multi-function-utility","multi-function GlideAjax utility"," you can adapt.",[37,1209,1211],{"id":1210},"getxmlanswer-vs-getxml","getXMLAnswer vs getXML",[12,1213,1214,1216,1217,1219],{},[16,1215,34],{}," is the shorter form and the one to use by default. ",[16,1218,137],{}," passes the full response object to the callback, and you read the answer yourself:",[225,1221,1223],{"className":227,"code":1222,"language":229,"meta":230,"style":230},"var ga = new GlideAjax('CallerInfoAjax');\nga.addParam('sysparm_name', 'getCallerInfo');\nga.addParam('sysparm_user_id', g_form.getValue('caller_id'));\nga.getXML(function(response) {\n  var answer = response.responseXML.documentElement.getAttribute('answer');\n  var result = JSON.parse(answer);\n  \u002F\u002F use result\n});\n",[16,1224,1225,1243,1260,1282,1300,1322,1339,1344],{"__ignoreMap":230},[234,1226,1227,1229,1231,1233,1235,1237,1239,1241],{"class":236,"line":237},[234,1228,241],{"class":240},[234,1230,898],{"class":244},[234,1232,248],{"class":240},[234,1234,509],{"class":240},[234,1236,905],{"class":254},[234,1238,327],{"class":244},[234,1240,910],{"class":330},[234,1242,334],{"class":244},[234,1244,1245,1248,1250,1252,1254,1256,1258],{"class":236,"line":261},[234,1246,1247],{"class":244},"ga.",[234,1249,920],{"class":254},[234,1251,327],{"class":244},[234,1253,925],{"class":330},[234,1255,813],{"class":244},[234,1257,930],{"class":330},[234,1259,334],{"class":244},[234,1261,1262,1264,1266,1268,1270,1273,1276,1278,1280],{"class":236,"line":286},[234,1263,1247],{"class":244},[234,1265,920],{"class":254},[234,1267,327],{"class":244},[234,1269,331],{"class":330},[234,1271,1272],{"class":244},", g_form.",[234,1274,1275],{"class":254},"getValue",[234,1277,327],{"class":244},[234,1279,868],{"class":330},[234,1281,1151],{"class":244},[234,1283,1284,1286,1289,1291,1293,1295,1298],{"class":236,"line":293},[234,1285,1247],{"class":244},[234,1287,1288],{"class":254},"getXML",[234,1290,327],{"class":244},[234,1292,302],{"class":240},[234,1294,327],{"class":244},[234,1296,1297],{"class":809},"response",[234,1299,834],{"class":244},[234,1301,1302,1304,1307,1309,1312,1315,1317,1320],{"class":236,"line":308},[234,1303,895],{"class":240},[234,1305,1306],{"class":244}," answer ",[234,1308,248],{"class":240},[234,1310,1311],{"class":244}," response.responseXML.documentElement.",[234,1313,1314],{"class":254},"getAttribute",[234,1316,327],{"class":244},[234,1318,1319],{"class":330},"'answer'",[234,1321,334],{"class":244},[234,1323,1324,1326,1329,1331,1333,1335,1337],{"class":236,"line":337},[234,1325,895],{"class":240},[234,1327,1328],{"class":244}," result ",[234,1330,248],{"class":240},[234,1332,398],{"class":264},[234,1334,268],{"class":244},[234,1336,991],{"class":254},[234,1338,994],{"class":244},[234,1340,1341],{"class":236,"line":342},[234,1342,1343],{"class":345},"  \u002F\u002F use result\n",[234,1345,1346],{"class":236,"line":349},[234,1347,680],{"class":244},[12,1349,1350,1351,1353,1354,268],{},"Both are asynchronous. You will see ",[16,1352,137],{}," in older code; there is no need to convert working scripts, but new scripts are easier to read with ",[16,1355,34],{},[37,1357,1359],{"id":1358},"why-not-getxmlwait","Why not getXMLWait",[12,1361,1362,1364,1365,1368],{},[16,1363,172],{}," waits for the server before the next line runs, then you read the result with ",[16,1366,1367],{},"ga.getAnswer()",". It looks convenient, especially in onSubmit scripts, but the browser is frozen while it waits and it does not work in Service Portal.",[12,1370,1371],{},"The usual reason people reach for it is an onSubmit check that needs server data. An asynchronous callback cannot cancel a submit because the function has already returned. Do the lookup earlier, in an onChange, store the result, and let onSubmit check it locally. Back the rule with a business rule on the server, because onSubmit does not run for list edits, imports or integrations.",[37,1373,1375],{"id":1374},"security-treat-every-call-as-public","Security: treat every call as public",[12,1377,1378],{},"A client-callable Script Include is an endpoint. Any logged-in user can call it from the browser console with any method name and any parameter values. Your client script being on an ITIL-only form does not restrict who can call the server side.",[12,1380,1381],{},"Checklist for every client-callable method:",[685,1383,1384,1391,1397,1406,1409,1416],{},[688,1385,1386,1387,1390],{},"Check the caller's roles with ",[16,1388,1389],{},"gs.hasRole()"," before returning anything sensitive.",[688,1392,1393,1394,1396],{},"Validate every ",[16,1395,30],{}," value: type, format and length.",[688,1398,1399,1400,1403,1404,268],{},"Query with ",[16,1401,1402],{},"GlideRecordSecure"," so access controls apply, not ",[16,1405,189],{},[688,1407,1408],{},"Return only the fields the client needs, never a whole record.",[688,1410,1411,1412,1415],{},"Never build an encoded query by concatenating raw input. Use ",[16,1413,1414],{},"addQuery()"," with separate field and value arguments.",[688,1417,1418,1419,1422,1423,1425],{},"Do not override ",[16,1420,1421],{},"isPublic()"," to return ",[16,1424,592],{}," unless the method must work for users who are not logged in and returns nothing sensitive.",[12,1427,1428,1429,1432,1433,1437],{},"If a GlideAjax call returns ",[16,1430,1431],{},"not_allowed"," for a user who should have access, the ",[57,1434,1436],{"href":1435},"\u002Fblog\u002Fservicenow-acl-troubleshooting-guide","ACL troubleshooting guide"," walks through finding the rule that blocks them.",[37,1439,1441],{"id":1440},"debugging-a-glideajax-call-that-returns-nothing","Debugging a GlideAjax call that returns nothing",[12,1443,1444],{},"Work through these in order:",[1446,1447,1448,1454,1466,1478,1488,1501],"ol",{},[688,1449,1450,1453],{},[45,1451,1452],{},"Client callable is not selected."," This is the most common cause.",[688,1455,1456,1459,1460,1462,1463,1465],{},[45,1457,1458],{},"Name mismatch."," The ",[16,1461,22],{}," class name and the ",[16,1464,26],{}," method name are case-sensitive. Check the scope prefix for scoped Script Includes.",[688,1467,1468,1471,1472,1474,1475,1477],{},[45,1469,1470],{},"Wrong base class."," The Script Include must extend ",[16,1473,18],{}," (or ",[16,1476,729],{}," in a scope).",[688,1479,1480,1483,1484,1487],{},[45,1481,1482],{},"The method returns nothing."," A method that ends without ",[16,1485,1486],{},"return"," sends an empty answer. Make sure every path returns a string.",[688,1489,1490,1493,1494,1497,1498,268],{},[45,1491,1492],{},"A server error."," Add ",[16,1495,1496],{},"gs.info('getCallerInfo called with ' + userId);"," at the top of the method and check ",[45,1499,1500],{},"System Logs > System Log > All",[688,1502,1503,1506,1507,1510],{},[45,1504,1505],{},"The request itself."," Open the browser developer tools, filter the network tab on ",[16,1508,1509],{},"xmlhttp.do",", and inspect the request parameters and the response body.",[37,1512,1514],{"id":1513},"keep-calls-cheap","Keep calls cheap",[12,1516,1517],{},"Each GlideAjax call is a round trip to the server. A few habits keep forms fast:",[685,1519,1520,1523,1529,1537],{},[688,1521,1522],{},"Return several values in one JSON response instead of making one call per field.",[688,1524,1525,1526,1528],{},"Skip the call when ",[16,1527,826],{}," is true or the new value is empty.",[688,1530,1531,1532,1536],{},"Cache values that rarely change. The ",[57,1533,1535],{"href":1534},"\u002Fblog\u002Fsession-caching-servicenow-script-includes-before-query-business-rules","session caching guide"," shows one way to do this on the server.",[688,1538,1539],{},"Group related methods in one Script Include per table or feature, so they are easy to find and review.",[37,1541,1543],{"id":1542},"summary-checklist","Summary checklist",[685,1545,1546,1555,1561,1567,1570,1578,1584],{},[688,1547,1548,1549,1551,1552,1554],{},"Script Include extends ",[16,1550,18],{}," and has ",[45,1553,212],{}," selected",[688,1556,1557,1558,1560],{},"Method names match ",[16,1559,26],{}," exactly",[688,1562,1563,1564,1566],{},"Custom parameters start with ",[16,1565,30],{}," and are validated",[688,1568,1569],{},"Methods return a string, using JSON for multiple values",[688,1571,1572,1573,1575,1576],{},"Client uses ",[16,1574,34],{}," with a callback, never ",[16,1577,172],{},[688,1579,1580,1581,1583],{},"Roles checked and ",[16,1582,1402],{}," used on the server",[688,1585,1586],{},"onSubmit rules are backed by a server-side business rule",[12,1588,1589,1590,1594,1595,268],{},"For more patterns that pair well with GlideAjax, see the ",[57,1591,1593],{"href":1592},"\u002Fblog\u002Fmastering-servicenow-scripting","ServiceNow coding best practices checklist"," and the ",[57,1596,1598],{"href":1597},"\u002Fblog\u002Fservicenow-business-rule-best-practices","business rule best practices guide",[1600,1601,1602],"style",{},"html pre.shiki code .sCydW, html code.shiki .sCydW{--shiki-default:#D73A49;--shiki-dark:#D73A49}html pre.shiki code .sKWpL, html code.shiki .sKWpL{--shiki-default:#24292E;--shiki-dark:#24292E}html pre.shiki code .se37E, html code.shiki .se37E{--shiki-default:#6F42C1;--shiki-dark:#6F42C1}html pre.shiki code .sMN4m, html code.shiki .sMN4m{--shiki-default:#005CC5;--shiki-dark:#005CC5}html pre.shiki code .sOTlB, html code.shiki .sOTlB{--shiki-default:#032F62;--shiki-dark:#032F62}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj_tP, html code.shiki .sj_tP{--shiki-default:#E36209;--shiki-dark:#E36209}",{"title":230,"searchDepth":261,"depth":261,"links":1604},[1605,1606,1607,1608,1609,1610,1611,1612,1613],{"id":39,"depth":261,"text":40},{"id":201,"depth":261,"text":202},{"id":784,"depth":261,"text":785},{"id":1210,"depth":261,"text":1211},{"id":1358,"depth":261,"text":1359},{"id":1374,"depth":261,"text":1375},{"id":1440,"depth":261,"text":1441},{"id":1513,"depth":261,"text":1514},{"id":1542,"depth":261,"text":1543},"2026-11-25","How to use GlideAjax in ServiceNow to call a client-callable Script Include from a client script, return JSON, and keep the call fast and secure.",null,"md",[1619,1622,1625,1628],{"question":1620,"answer":1621},"What is GlideAjax in ServiceNow?","GlideAjax is a client-side API that sends a request from the browser to a client-callable Script Include on the server and hands the result back to a callback function. It lets a client script use server data without client-side GlideRecord.",{"question":1623,"answer":1624},"Why does my GlideAjax call return null?","The most common causes are: the Client callable box is not selected on the Script Include, the sysparm_name value does not match a method name, the Script Include does not extend AbstractAjaxProcessor, the class name is misspelled or missing its scope prefix, or the method returns nothing. Add gs.info() lines to the method and check the system log to see whether it runs at all.",{"question":1626,"answer":1627},"Should I use getXMLAnswer or getXMLWait?","Use getXMLAnswer with a callback. It is asynchronous, so the form stays responsive. getXMLWait is synchronous, freezes the browser until the server replies, and is not supported in Service Portal.",{"question":1629,"answer":1630},"Is a client-callable Script Include secure by default?","No. Any logged-in user can call it from the browser with any parameter values, regardless of which form your client script runs on. Check roles inside the method, validate every parameter, and query with GlideRecordSecure so access controls apply.",{},"\u002Fblog\u002Fservicenow-glideajax-guide",{"title":5,"description":1615},"\u002Fsocial-card.png","blog\u002Fservicenow-glideajax-guide",[1637,1638,1639,1640],"glideajax","client scripts","script includes","scripting","nFYpk-4nXuyGgZlJP1unkZ6ApYpHbnCd2h6-gjWJ-CQ",[1643,1649,1664],{"path":59,"title":1644,"headline":1644,"description":1645,"date":1646,"tags":1647},"ServiceNow Client Script Best Practices: onLoad, onChange, onSubmit","ServiceNow client script best practices for onLoad, onChange and onSubmit: when to use each type, how to avoid slow forms, and safe GlideAjax patterns.","2026-11-20",[1638,1640,1648],"forms",{"path":1650,"title":1651,"headline":1652,"description":1653,"date":1654,"tags":1655},"\u002Fblog\u002Fintroducing-servicenow-scripts-library","Introducing the ServiceNow Scripts Library: Your Go-To Resource for Ready-to-Use Code | SN-Tricks","Introducing the ServiceNow Scripts Library: Your Go-To Resource for Ready-to-Use Code","Discover the new ServiceNow Scripts Library on SN-Tricks - a searchable collection of battle-tested Client Scripts, Business Rules, and Script Includes with copy-paste functionality.","2025-11-14T10:00:00",[1656,1657,1658,1659,1660,1661,1662,1663],"ServiceNow","Scripts","Client Scripts","Business Rules","Script Includes","Development","Code Library","Best Practices",{"path":1534,"title":1665,"headline":1666,"description":1667,"date":1668,"tags":1669},"Session Caching in ServiceNow with Script Includes and Before Query Business Rules | SN-Tricks","Session Caching in ServiceNow: Boost Performance with Script Includes and Before Query Business Rules","Learn how to dramatically improve ServiceNow performance using session caching with Script Includes and Before Query Business Rules","2025-10-19T10:00:00",[1656,1670,1671,1672,1659,1673,1663],"Performance","Caching","Script Include","Optimization",1795568764172]