[{"data":1,"prerenderedAt":1698},["ShallowReactive",2],{"page-\u002Fblog\u002Fservicenow-script-include-patterns\u002F":3,"related-\u002Fblog\u002Fservicenow-script-include-patterns\u002F":1669},{"id":4,"title":5,"author":6,"authorUrl":7,"body":8,"date":1642,"dateUpdated":1642,"description":1643,"dqid":1642,"excerpt":1644,"extension":1645,"faq":1646,"headline":5,"meta":1659,"navigation":428,"path":1660,"seo":1661,"socialImage":1662,"stem":1663,"tags":1664,"__hash__":1668},"content\u002Fblog\u002Fservicenow-script-include-patterns.md","ServiceNow Script Include Patterns: Classless, Class-Based and Client-Callable","Gabriel Fordan","https:\u002F\u002Fsn-tricks.com\u002Fabout",{"type":9,"value":10,"toc":1631},"minimark",[11,15,18,23,36,82,85,89,92,98,277,280,325,328,332,339,344,664,670,737,740,763,782,786,793,1051,1060,1076,1080,1096,1358,1364,1384,1388,1391,1412,1415,1419,1511,1515,1559,1563,1620,1627],[12,13,14],"p",{},"A ServiceNow Script Include is reusable server-side code, and there are four patterns worth knowing: a classless (on-demand) function, a class-based utility, a class that extends another, and a client-callable class for GlideAjax. Pick classless for one small function, a class for a group of related methods, extension to reuse a base class, and client-callable only when a browser script must reach the server.",[12,16,17],{},"This guide shows each pattern with working code, the naming rules that trip people up, and how scope and security settings change who can call your code.",[19,20,22],"h2",{"id":21},"where-script-includes-live","Where Script Includes live",[12,24,25,26,30,31,35],{},"Open ",[27,28,29],"strong",{},"System Definition > Script Includes"," to create one. The records are stored in the ",[32,33,34],"code",{},"sys_script_include"," table. The fields that matter most:",[37,38,39,46,56,62,76],"ul",{},[40,41,42,45],"li",{},[27,43,44],{},"Name",": what callers use. For both classless and class-based patterns it must match the function or class name in the script.",[40,47,48,51,52,55],{},[27,49,50],{},"API Name",": the scope-qualified name, such as ",[32,53,54],{},"x_acme_app.AssetUtils",". ServiceNow fills it in.",[40,57,58,61],{},[27,59,60],{},"Accessible from",": either \"This application scope only\" or \"All application scopes\".",[40,63,64,67,68,71,72,75],{},[27,65,66],{},"Client callable",": allows GlideAjax calls. On Xanadu and later this checkbox is labelled ",[27,69,70],{},"Glide AJAX enabled",", and a separate ",[27,73,74],{},"Sandbox enabled"," option controls calls from the script sandbox.",[40,77,78,81],{},[27,79,80],{},"Active",": inactive Script Includes cannot be called.",[12,83,84],{},"Script Includes only load when a script uses them, so a large library does not slow down every transaction.",[19,86,88],{"id":87},"pattern-1-classless-on-demand-function","Pattern 1: Classless (on-demand) function",[12,90,91],{},"A classless Script Include defines one function. ServiceNow also calls this an on-demand Script Include. The Script Include name must exactly match the function name.",[12,93,94,95],{},"Name: ",[32,96,97],{},"getActiveGroupCount",[99,100,105],"pre",{"className":101,"code":102,"language":103,"meta":104,"style":104},"language-javascript shiki shiki-themes github-light github-light","function getActiveGroupCount(userId) {\n  var count = new GlideAggregate('sys_user_grmember');\n  count.addQuery('user', userId);\n  count.addQuery('group.active', true);\n  count.addAggregate('COUNT');\n  count.query();\n  return count.next() ? parseInt(count.getAggregate('COUNT'), 10) : 0;\n}\n","javascript","",[32,106,107,131,158,175,196,211,222,271],{"__ignoreMap":104},[108,109,112,116,120,124,128],"span",{"class":110,"line":111},"line",1,[108,113,115],{"class":114},"sCydW","function",[108,117,119],{"class":118},"se37E"," getActiveGroupCount",[108,121,123],{"class":122},"sKWpL","(",[108,125,127],{"class":126},"sj_tP","userId",[108,129,130],{"class":122},") {\n",[108,132,134,137,140,143,146,149,151,155],{"class":110,"line":133},2,[108,135,136],{"class":114},"  var",[108,138,139],{"class":122}," count ",[108,141,142],{"class":114},"=",[108,144,145],{"class":114}," new",[108,147,148],{"class":118}," GlideAggregate",[108,150,123],{"class":122},[108,152,154],{"class":153},"sOTlB","'sys_user_grmember'",[108,156,157],{"class":122},");\n",[108,159,161,164,167,169,172],{"class":110,"line":160},3,[108,162,163],{"class":122},"  count.",[108,165,166],{"class":118},"addQuery",[108,168,123],{"class":122},[108,170,171],{"class":153},"'user'",[108,173,174],{"class":122},", userId);\n",[108,176,178,180,182,184,187,190,194],{"class":110,"line":177},4,[108,179,163],{"class":122},[108,181,166],{"class":118},[108,183,123],{"class":122},[108,185,186],{"class":153},"'group.active'",[108,188,189],{"class":122},", ",[108,191,193],{"class":192},"sMN4m","true",[108,195,157],{"class":122},[108,197,199,201,204,206,209],{"class":110,"line":198},5,[108,200,163],{"class":122},[108,202,203],{"class":118},"addAggregate",[108,205,123],{"class":122},[108,207,208],{"class":153},"'COUNT'",[108,210,157],{"class":122},[108,212,214,216,219],{"class":110,"line":213},6,[108,215,163],{"class":122},[108,217,218],{"class":118},"query",[108,220,221],{"class":122},"();\n",[108,223,225,228,231,234,237,240,243,246,249,251,253,256,259,262,265,268],{"class":110,"line":224},7,[108,226,227],{"class":114},"  return",[108,229,230],{"class":122}," count.",[108,232,233],{"class":118},"next",[108,235,236],{"class":122},"() ",[108,238,239],{"class":114},"?",[108,241,242],{"class":118}," parseInt",[108,244,245],{"class":122},"(count.",[108,247,248],{"class":118},"getAggregate",[108,250,123],{"class":122},[108,252,208],{"class":153},[108,254,255],{"class":122},"), ",[108,257,258],{"class":192},"10",[108,260,261],{"class":122},") ",[108,263,264],{"class":114},":",[108,266,267],{"class":192}," 0",[108,269,270],{"class":122},";\n",[108,272,274],{"class":110,"line":273},8,[108,275,276],{"class":122},"}\n",[12,278,279],{},"Call it from any server-side script in the same scope by name:",[99,281,283],{"className":101,"code":282,"language":103,"meta":104,"style":104},"var groups = getActiveGroupCount(gs.getUserID());\ngs.info('Active groups for current user: ' + groups);\n",[32,284,285,306],{"__ignoreMap":104},[108,286,287,290,293,295,297,300,303],{"class":110,"line":111},[108,288,289],{"class":114},"var",[108,291,292],{"class":122}," groups ",[108,294,142],{"class":114},[108,296,119],{"class":118},[108,298,299],{"class":122},"(gs.",[108,301,302],{"class":118},"getUserID",[108,304,305],{"class":122},"());\n",[108,307,308,311,314,316,319,322],{"class":110,"line":133},[108,309,310],{"class":122},"gs.",[108,312,313],{"class":118},"info",[108,315,123],{"class":122},[108,317,318],{"class":153},"'Active groups for current user: '",[108,320,321],{"class":114}," +",[108,323,324],{"class":122}," groups);\n",[12,326,327],{},"Classless works well for one small helper. It stops working well once you have several related functions, because each needs its own record and nothing groups them. It also cannot be called through GlideAjax.",[19,329,331],{"id":330},"pattern-2-class-based-utility","Pattern 2: Class-based utility",[12,333,334,335,338],{},"This is the pattern ServiceNow generates when you create a new Script Include. It groups related methods under one name and lets you pass shared state through ",[32,336,337],{},"initialize",".",[12,340,94,341],{},[32,342,343],{},"IncidentUtils",[99,345,347],{"className":101,"code":346,"language":103,"meta":104,"style":104},"var IncidentUtils = Class.create();\nIncidentUtils.prototype = {\n  initialize: function(maxRows) {\n    this.maxRows = maxRows || 100;\n  },\n\n  getOpenByGroup: function(groupId) {\n    var result = [];\n    var inc = new GlideRecord('incident');\n    inc.addActiveQuery();\n    inc.addQuery('assignment_group', groupId);\n    inc.orderByDesc('sys_created_on');\n    inc.setLimit(this.maxRows);\n    inc.query();\n    while (inc.next()) {\n      result.push(inc.getValue('number'));\n    }\n    return result;\n  },\n\n  isUnassigned: function(incGr) {\n    return incGr.assigned_to.nil();\n  },\n\n  type: 'IncidentUtils'\n};\n",[32,348,349,366,381,398,419,424,430,446,459,481,492,507,522,538,547,561,584,590,599,604,609,626,639,644,649,658],{"__ignoreMap":104},[108,350,351,353,356,358,361,364],{"class":110,"line":111},[108,352,289],{"class":114},[108,354,355],{"class":122}," IncidentUtils ",[108,357,142],{"class":114},[108,359,360],{"class":122}," Class.",[108,362,363],{"class":118},"create",[108,365,221],{"class":122},[108,367,368,370,372,375,378],{"class":110,"line":133},[108,369,343],{"class":192},[108,371,338],{"class":122},[108,373,374],{"class":192},"prototype",[108,376,377],{"class":114}," =",[108,379,380],{"class":122}," {\n",[108,382,383,386,389,391,393,396],{"class":110,"line":160},[108,384,385],{"class":118},"  initialize",[108,387,388],{"class":122},": ",[108,390,115],{"class":114},[108,392,123],{"class":122},[108,394,395],{"class":126},"maxRows",[108,397,130],{"class":122},[108,399,400,403,406,408,411,414,417],{"class":110,"line":177},[108,401,402],{"class":192},"    this",[108,404,405],{"class":122},".maxRows ",[108,407,142],{"class":114},[108,409,410],{"class":122}," maxRows ",[108,412,413],{"class":114},"||",[108,415,416],{"class":192}," 100",[108,418,270],{"class":122},[108,420,421],{"class":110,"line":198},[108,422,423],{"class":122},"  },\n",[108,425,426],{"class":110,"line":213},[108,427,429],{"emptyLinePlaceholder":428},true,"\n",[108,431,432,435,437,439,441,444],{"class":110,"line":224},[108,433,434],{"class":118},"  getOpenByGroup",[108,436,388],{"class":122},[108,438,115],{"class":114},[108,440,123],{"class":122},[108,442,443],{"class":126},"groupId",[108,445,130],{"class":122},[108,447,448,451,454,456],{"class":110,"line":273},[108,449,450],{"class":114},"    var",[108,452,453],{"class":122}," result ",[108,455,142],{"class":114},[108,457,458],{"class":122}," [];\n",[108,460,462,464,467,469,471,474,476,479],{"class":110,"line":461},9,[108,463,450],{"class":114},[108,465,466],{"class":122}," inc ",[108,468,142],{"class":114},[108,470,145],{"class":114},[108,472,473],{"class":118}," GlideRecord",[108,475,123],{"class":122},[108,477,478],{"class":153},"'incident'",[108,480,157],{"class":122},[108,482,484,487,490],{"class":110,"line":483},10,[108,485,486],{"class":122},"    inc.",[108,488,489],{"class":118},"addActiveQuery",[108,491,221],{"class":122},[108,493,495,497,499,501,504],{"class":110,"line":494},11,[108,496,486],{"class":122},[108,498,166],{"class":118},[108,500,123],{"class":122},[108,502,503],{"class":153},"'assignment_group'",[108,505,506],{"class":122},", groupId);\n",[108,508,510,512,515,517,520],{"class":110,"line":509},12,[108,511,486],{"class":122},[108,513,514],{"class":118},"orderByDesc",[108,516,123],{"class":122},[108,518,519],{"class":153},"'sys_created_on'",[108,521,157],{"class":122},[108,523,525,527,530,532,535],{"class":110,"line":524},13,[108,526,486],{"class":122},[108,528,529],{"class":118},"setLimit",[108,531,123],{"class":122},[108,533,534],{"class":192},"this",[108,536,537],{"class":122},".maxRows);\n",[108,539,541,543,545],{"class":110,"line":540},14,[108,542,486],{"class":122},[108,544,218],{"class":118},[108,546,221],{"class":122},[108,548,550,553,556,558],{"class":110,"line":549},15,[108,551,552],{"class":114},"    while",[108,554,555],{"class":122}," (inc.",[108,557,233],{"class":118},[108,559,560],{"class":122},"()) {\n",[108,562,564,567,570,573,576,578,581],{"class":110,"line":563},16,[108,565,566],{"class":122},"      result.",[108,568,569],{"class":118},"push",[108,571,572],{"class":122},"(inc.",[108,574,575],{"class":118},"getValue",[108,577,123],{"class":122},[108,579,580],{"class":153},"'number'",[108,582,583],{"class":122},"));\n",[108,585,587],{"class":110,"line":586},17,[108,588,589],{"class":122},"    }\n",[108,591,593,596],{"class":110,"line":592},18,[108,594,595],{"class":114},"    return",[108,597,598],{"class":122}," result;\n",[108,600,602],{"class":110,"line":601},19,[108,603,423],{"class":122},[108,605,607],{"class":110,"line":606},20,[108,608,429],{"emptyLinePlaceholder":428},[108,610,612,615,617,619,621,624],{"class":110,"line":611},21,[108,613,614],{"class":118},"  isUnassigned",[108,616,388],{"class":122},[108,618,115],{"class":114},[108,620,123],{"class":122},[108,622,623],{"class":126},"incGr",[108,625,130],{"class":122},[108,627,629,631,634,637],{"class":110,"line":628},22,[108,630,595],{"class":114},[108,632,633],{"class":122}," incGr.assigned_to.",[108,635,636],{"class":118},"nil",[108,638,221],{"class":122},[108,640,642],{"class":110,"line":641},23,[108,643,423],{"class":122},[108,645,647],{"class":110,"line":646},24,[108,648,429],{"emptyLinePlaceholder":428},[108,650,652,655],{"class":110,"line":651},25,[108,653,654],{"class":122},"  type: ",[108,656,657],{"class":153},"'IncidentUtils'\n",[108,659,661],{"class":110,"line":660},26,[108,662,663],{"class":122},"};\n",[12,665,666,667,264],{},"Call it with ",[32,668,669],{},"new",[99,671,673],{"className":101,"code":672,"language":103,"meta":104,"style":104},"var utils = new IncidentUtils(20);\nvar numbers = utils.getOpenByGroup('\u003Cassignment_group_sys_id>');\ngs.info(numbers.join(', '));\n",[32,674,675,696,718],{"__ignoreMap":104},[108,676,677,679,682,684,686,689,691,694],{"class":110,"line":111},[108,678,289],{"class":114},[108,680,681],{"class":122}," utils ",[108,683,142],{"class":114},[108,685,145],{"class":114},[108,687,688],{"class":118}," IncidentUtils",[108,690,123],{"class":122},[108,692,693],{"class":192},"20",[108,695,157],{"class":122},[108,697,698,700,703,705,708,711,713,716],{"class":110,"line":133},[108,699,289],{"class":114},[108,701,702],{"class":122}," numbers ",[108,704,142],{"class":114},[108,706,707],{"class":122}," utils.",[108,709,710],{"class":118},"getOpenByGroup",[108,712,123],{"class":122},[108,714,715],{"class":153},"'\u003Cassignment_group_sys_id>'",[108,717,157],{"class":122},[108,719,720,722,724,727,730,732,735],{"class":110,"line":160},[108,721,310],{"class":122},[108,723,313],{"class":118},[108,725,726],{"class":122},"(numbers.",[108,728,729],{"class":118},"join",[108,731,123],{"class":122},[108,733,734],{"class":153},"', '",[108,736,583],{"class":122},[12,738,739],{},"Some rules that save debugging time:",[37,741,742,749,756],{},[40,743,744,745,748],{},"Keep the ",[32,746,747],{},"type"," property equal to the class name. Platform code and logging use it.",[40,750,751,752,755],{},"Pass records in as arguments rather than relying on a global ",[32,753,754],{},"current",". The method then works the same from a business rule, a flow script step or a background script.",[40,757,758,759,762],{},"Prefix internal helpers with an underscore, such as ",[32,760,761],{},"_buildQuery",". It is a convention, not enforcement, but reviewers will know not to call them.",[12,764,765,766,771,772,776,777,781],{},"The ",[767,768,770],"a",{"href":769},"\u002Fblog\u002Fservicenow-gliderecord-query-performance","GlideRecord query performance guide"," covers how to keep the queries inside these methods fast. For ready-made examples of this pattern, see the ",[767,773,775],{"href":774},"\u002Fscripts\u002Fuser-group-utilities","user and group utilities"," and ",[767,778,780],{"href":779},"\u002Fscripts\u002Frecord-query-utility","record query utility"," scripts.",[19,783,785],{"id":784},"pattern-3-extending-a-class","Pattern 3: Extending a class",[12,787,788,789,792],{},"Use ",[32,790,791],{},"Object.extendsObject()"," when a new class should reuse another class's methods and override only some of them.",[99,794,796],{"className":101,"code":795,"language":103,"meta":104,"style":104},"var MajorIncidentUtils = Class.create();\nMajorIncidentUtils.prototype = Object.extendsObject(IncidentUtils, {\n\n  initialize: function(maxRows) {\n    IncidentUtils.prototype.initialize.call(this, maxRows);\n    this.priorityFilter = '1';\n  },\n\n  getOpenByGroup: function(groupId) {\n    var result = [];\n    var inc = new GlideRecord('incident');\n    inc.addActiveQuery();\n    inc.addQuery('assignment_group', groupId);\n    inc.addQuery('priority', this.priorityFilter);\n    inc.setLimit(this.maxRows);\n    inc.query();\n    while (inc.next()) {\n      result.push(inc.getValue('number'));\n    }\n    return result;\n  },\n\n  type: 'MajorIncidentUtils'\n});\n",[32,797,798,813,833,837,851,873,887,891,895,909,919,937,945,957,975,987,995,1005,1021,1025,1031,1035,1039,1046],{"__ignoreMap":104},[108,799,800,802,805,807,809,811],{"class":110,"line":111},[108,801,289],{"class":114},[108,803,804],{"class":122}," MajorIncidentUtils ",[108,806,142],{"class":114},[108,808,360],{"class":122},[108,810,363],{"class":118},[108,812,221],{"class":122},[108,814,815,818,820,822,824,827,830],{"class":110,"line":133},[108,816,817],{"class":192},"MajorIncidentUtils",[108,819,338],{"class":122},[108,821,374],{"class":192},[108,823,377],{"class":114},[108,825,826],{"class":122}," Object.",[108,828,829],{"class":118},"extendsObject",[108,831,832],{"class":122},"(IncidentUtils, {\n",[108,834,835],{"class":110,"line":160},[108,836,429],{"emptyLinePlaceholder":428},[108,838,839,841,843,845,847,849],{"class":110,"line":177},[108,840,385],{"class":118},[108,842,388],{"class":122},[108,844,115],{"class":114},[108,846,123],{"class":122},[108,848,395],{"class":126},[108,850,130],{"class":122},[108,852,853,856,858,860,863,866,868,870],{"class":110,"line":198},[108,854,855],{"class":192},"    IncidentUtils",[108,857,338],{"class":122},[108,859,374],{"class":192},[108,861,862],{"class":122},".initialize.",[108,864,865],{"class":118},"call",[108,867,123],{"class":122},[108,869,534],{"class":192},[108,871,872],{"class":122},", maxRows);\n",[108,874,875,877,880,882,885],{"class":110,"line":213},[108,876,402],{"class":192},[108,878,879],{"class":122},".priorityFilter ",[108,881,142],{"class":114},[108,883,884],{"class":153}," '1'",[108,886,270],{"class":122},[108,888,889],{"class":110,"line":224},[108,890,423],{"class":122},[108,892,893],{"class":110,"line":273},[108,894,429],{"emptyLinePlaceholder":428},[108,896,897,899,901,903,905,907],{"class":110,"line":461},[108,898,434],{"class":118},[108,900,388],{"class":122},[108,902,115],{"class":114},[108,904,123],{"class":122},[108,906,443],{"class":126},[108,908,130],{"class":122},[108,910,911,913,915,917],{"class":110,"line":483},[108,912,450],{"class":114},[108,914,453],{"class":122},[108,916,142],{"class":114},[108,918,458],{"class":122},[108,920,921,923,925,927,929,931,933,935],{"class":110,"line":494},[108,922,450],{"class":114},[108,924,466],{"class":122},[108,926,142],{"class":114},[108,928,145],{"class":114},[108,930,473],{"class":118},[108,932,123],{"class":122},[108,934,478],{"class":153},[108,936,157],{"class":122},[108,938,939,941,943],{"class":110,"line":509},[108,940,486],{"class":122},[108,942,489],{"class":118},[108,944,221],{"class":122},[108,946,947,949,951,953,955],{"class":110,"line":524},[108,948,486],{"class":122},[108,950,166],{"class":118},[108,952,123],{"class":122},[108,954,503],{"class":153},[108,956,506],{"class":122},[108,958,959,961,963,965,968,970,972],{"class":110,"line":540},[108,960,486],{"class":122},[108,962,166],{"class":118},[108,964,123],{"class":122},[108,966,967],{"class":153},"'priority'",[108,969,189],{"class":122},[108,971,534],{"class":192},[108,973,974],{"class":122},".priorityFilter);\n",[108,976,977,979,981,983,985],{"class":110,"line":549},[108,978,486],{"class":122},[108,980,529],{"class":118},[108,982,123],{"class":122},[108,984,534],{"class":192},[108,986,537],{"class":122},[108,988,989,991,993],{"class":110,"line":563},[108,990,486],{"class":122},[108,992,218],{"class":118},[108,994,221],{"class":122},[108,996,997,999,1001,1003],{"class":110,"line":586},[108,998,552],{"class":114},[108,1000,555],{"class":122},[108,1002,233],{"class":118},[108,1004,560],{"class":122},[108,1006,1007,1009,1011,1013,1015,1017,1019],{"class":110,"line":592},[108,1008,566],{"class":122},[108,1010,569],{"class":118},[108,1012,572],{"class":122},[108,1014,575],{"class":118},[108,1016,123],{"class":122},[108,1018,580],{"class":153},[108,1020,583],{"class":122},[108,1022,1023],{"class":110,"line":601},[108,1024,589],{"class":122},[108,1026,1027,1029],{"class":110,"line":606},[108,1028,595],{"class":114},[108,1030,598],{"class":122},[108,1032,1033],{"class":110,"line":611},[108,1034,423],{"class":122},[108,1036,1037],{"class":110,"line":628},[108,1038,429],{"emptyLinePlaceholder":428},[108,1040,1041,1043],{"class":110,"line":641},[108,1042,654],{"class":122},[108,1044,1045],{"class":153},"'MajorIncidentUtils'\n",[108,1047,1048],{"class":110,"line":646},[108,1049,1050],{"class":122},"});\n",[12,1052,1053,1056,1057,1059],{},[32,1054,1055],{},"isUnassigned"," is inherited unchanged. If you override ",[32,1058,337],{},", call the parent's version as shown, or the parent's setup never runs.",[12,1061,1062,1063,1066,1067,1070,1071,1075],{},"Extension is also how you customise many base system Script Includes safely. Several baseline classes ship as an empty ",[32,1064,1065],{},"..."," subclass of an ",[32,1068,1069],{},"...SNC"," class, and the empty class is the one meant for your changes. Editing the subclass instead of the original keeps your changes out of the way during upgrades. The ",[767,1072,1074],{"href":1073},"\u002Fblog\u002Fservicenow-upgrade-skipped-changes-checklist","upgrade skipped changes checklist"," explains why that matters.",[19,1077,1079],{"id":1078},"pattern-4-client-callable-for-glideajax","Pattern 4: Client-callable for GlideAjax",[12,1081,1082,1083,1086,1087,1089,1090,1092,1093,338],{},"When a client script needs server data, the Script Include must extend ",[32,1084,1085],{},"AbstractAjaxProcessor"," and have ",[27,1088,66],{}," (or ",[27,1091,70],{},") selected. In a scoped application, extend ",[32,1094,1095],{},"global.AbstractAjaxProcessor",[99,1097,1099],{"className":101,"code":1098,"language":103,"meta":104,"style":104},"var IncidentAjax = Class.create();\nIncidentAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {\n\n  getOpenCount: function() {\n    var groupId = this.getParameter('sysparm_group_id');\n    if (!groupId || !\u002F^[0-9a-f]{32}$\u002F.test(groupId)) {\n      return JSON.stringify({ ok: false, error: 'invalid_group' });\n    }\n    if (!gs.hasRole('itil')) {\n      return JSON.stringify({ ok: false, error: 'not_allowed' });\n    }\n    var utils = new IncidentUtils(500);\n    return JSON.stringify({ ok: true, count: utils.getOpenByGroup(groupId).length });\n  },\n\n  type: 'IncidentAjax'\n});\n",[32,1100,1101,1116,1134,1138,1150,1174,1215,1243,1247,1268,1289,1293,1312,1339,1343,1347,1354],{"__ignoreMap":104},[108,1102,1103,1105,1108,1110,1112,1114],{"class":110,"line":111},[108,1104,289],{"class":114},[108,1106,1107],{"class":122}," IncidentAjax ",[108,1109,142],{"class":114},[108,1111,360],{"class":122},[108,1113,363],{"class":118},[108,1115,221],{"class":122},[108,1117,1118,1121,1123,1125,1127,1129,1131],{"class":110,"line":133},[108,1119,1120],{"class":192},"IncidentAjax",[108,1122,338],{"class":122},[108,1124,374],{"class":192},[108,1126,377],{"class":114},[108,1128,826],{"class":122},[108,1130,829],{"class":118},[108,1132,1133],{"class":122},"(AbstractAjaxProcessor, {\n",[108,1135,1136],{"class":110,"line":160},[108,1137,429],{"emptyLinePlaceholder":428},[108,1139,1140,1143,1145,1147],{"class":110,"line":177},[108,1141,1142],{"class":118},"  getOpenCount",[108,1144,388],{"class":122},[108,1146,115],{"class":114},[108,1148,1149],{"class":122},"() {\n",[108,1151,1152,1154,1157,1159,1162,1164,1167,1169,1172],{"class":110,"line":198},[108,1153,450],{"class":114},[108,1155,1156],{"class":122}," groupId ",[108,1158,142],{"class":114},[108,1160,1161],{"class":192}," this",[108,1163,338],{"class":122},[108,1165,1166],{"class":118},"getParameter",[108,1168,123],{"class":122},[108,1170,1171],{"class":153},"'sysparm_group_id'",[108,1173,157],{"class":122},[108,1175,1176,1179,1182,1185,1188,1190,1193,1196,1199,1202,1205,1207,1209,1212],{"class":110,"line":213},[108,1177,1178],{"class":114},"    if",[108,1180,1181],{"class":122}," (",[108,1183,1184],{"class":114},"!",[108,1186,1187],{"class":122},"groupId ",[108,1189,413],{"class":114},[108,1191,1192],{"class":114}," !",[108,1194,1195],{"class":153},"\u002F",[108,1197,1198],{"class":114},"^",[108,1200,1201],{"class":192},"[0-9a-f]",[108,1203,1204],{"class":114},"{32}$",[108,1206,1195],{"class":153},[108,1208,338],{"class":122},[108,1210,1211],{"class":118},"test",[108,1213,1214],{"class":122},"(groupId)) {\n",[108,1216,1217,1220,1223,1225,1228,1231,1234,1237,1240],{"class":110,"line":224},[108,1218,1219],{"class":114},"      return",[108,1221,1222],{"class":192}," JSON",[108,1224,338],{"class":122},[108,1226,1227],{"class":118},"stringify",[108,1229,1230],{"class":122},"({ ok: ",[108,1232,1233],{"class":192},"false",[108,1235,1236],{"class":122},", error: ",[108,1238,1239],{"class":153},"'invalid_group'",[108,1241,1242],{"class":122}," });\n",[108,1244,1245],{"class":110,"line":273},[108,1246,589],{"class":122},[108,1248,1249,1251,1253,1255,1257,1260,1262,1265],{"class":110,"line":461},[108,1250,1178],{"class":114},[108,1252,1181],{"class":122},[108,1254,1184],{"class":114},[108,1256,310],{"class":122},[108,1258,1259],{"class":118},"hasRole",[108,1261,123],{"class":122},[108,1263,1264],{"class":153},"'itil'",[108,1266,1267],{"class":122},")) {\n",[108,1269,1270,1272,1274,1276,1278,1280,1282,1284,1287],{"class":110,"line":483},[108,1271,1219],{"class":114},[108,1273,1222],{"class":192},[108,1275,338],{"class":122},[108,1277,1227],{"class":118},[108,1279,1230],{"class":122},[108,1281,1233],{"class":192},[108,1283,1236],{"class":122},[108,1285,1286],{"class":153},"'not_allowed'",[108,1288,1242],{"class":122},[108,1290,1291],{"class":110,"line":494},[108,1292,589],{"class":122},[108,1294,1295,1297,1299,1301,1303,1305,1307,1310],{"class":110,"line":509},[108,1296,450],{"class":114},[108,1298,681],{"class":122},[108,1300,142],{"class":114},[108,1302,145],{"class":114},[108,1304,688],{"class":118},[108,1306,123],{"class":122},[108,1308,1309],{"class":192},"500",[108,1311,157],{"class":122},[108,1313,1314,1316,1318,1320,1322,1324,1326,1329,1331,1334,1337],{"class":110,"line":524},[108,1315,595],{"class":114},[108,1317,1222],{"class":192},[108,1319,338],{"class":122},[108,1321,1227],{"class":118},[108,1323,1230],{"class":122},[108,1325,193],{"class":192},[108,1327,1328],{"class":122},", count: utils.",[108,1330,710],{"class":118},[108,1332,1333],{"class":122},"(groupId).",[108,1335,1336],{"class":192},"length",[108,1338,1242],{"class":122},[108,1340,1341],{"class":110,"line":540},[108,1342,423],{"class":122},[108,1344,1345],{"class":110,"line":549},[108,1346,429],{"emptyLinePlaceholder":428},[108,1348,1349,1351],{"class":110,"line":563},[108,1350,654],{"class":122},[108,1352,1353],{"class":153},"'IncidentAjax'\n",[108,1355,1356],{"class":110,"line":586},[108,1357,1050],{"class":122},[12,1359,1360,1361,1363],{},"Notice that the Ajax class only validates input, checks the role and delegates to ",[32,1362,343],{},". That split matters. Every public method on a client-callable class can be invoked by anyone who can send a request from a browser session, so keep it thin and keep server-only logic in a class that is not client callable.",[12,1365,765,1366,1370,1371,1374,1375,1378,1379,1383],{},[767,1367,1369],{"href":1368},"\u002Fblog\u002Fservicenow-glideajax-guide","GlideAjax guide"," covers the client side: ",[32,1372,1373],{},"getXMLAnswer",", parsing JSON and why to avoid ",[32,1376,1377],{},"getXMLWait",". The ",[767,1380,1382],{"href":1381},"\u002Fscripts\u002Fglideajax-multi-function-utility","GlideAjax multi-function utility"," script shows one class serving several related calls.",[19,1385,1387],{"id":1386},"calling-across-scopes","Calling across scopes",[12,1389,1390],{},"A Script Include in a scoped application is private by default. To share it:",[1392,1393,1394,1400,1406],"ol",{},[40,1395,1396,1397,1399],{},"Set ",[27,1398,60],{}," to \"All application scopes\".",[40,1401,1402,1403,338],{},"Call it with the scope prefix: ",[32,1404,1405],{},"new x_acme_app.AssetUtils()",[40,1407,1408,1409,338],{},"From a scoped app, call global code with ",[32,1410,1411],{},"new global.IncidentUtils()",[12,1413,1414],{},"Calls across scopes are also subject to the target application's cross-scope privilege settings, so a call that works in one scope may need a privilege record in another. Keep scoped Script Includes private unless another application really needs them.",[19,1416,1418],{"id":1417},"choosing-a-pattern","Choosing a pattern",[1420,1421,1422,1438],"table",{},[1423,1424,1425],"thead",{},[1426,1427,1428,1432,1435],"tr",{},[1429,1430,1431],"th",{},"Need",[1429,1433,1434],{},"Pattern",[1429,1436,1437],{},"Called with",[1439,1440,1441,1455,1468,1481,1496],"tbody",{},[1426,1442,1443,1447,1450],{},[1444,1445,1446],"td",{},"One small server function",[1444,1448,1449],{},"Classless",[1444,1451,1452],{},[32,1453,1454],{},"myFunction(args)",[1426,1456,1457,1460,1463],{},[1444,1458,1459],{},"Group of related server methods",[1444,1461,1462],{},"Class-based",[1444,1464,1465],{},[32,1466,1467],{},"new MyUtils().method()",[1426,1469,1470,1473,1476],{},[1444,1471,1472],{},"Reuse a class and change part of it",[1444,1474,1475],{},"Extended class",[1444,1477,1478],{},[32,1479,1480],{},"new MySubUtils().method()",[1426,1482,1483,1486,1491],{},[1444,1484,1485],{},"Client script needs server data",[1444,1487,1488,1489],{},"Client-callable, extends ",[32,1490,1085],{},[1444,1492,1493],{},[32,1494,1495],{},"new GlideAjax('MyAjax')",[1426,1497,1498,1501,1504],{},[1444,1499,1500],{},"Value cached for the user's session",[1444,1502,1503],{},"Class-based, called from a before query rule",[1444,1505,1506,1507],{},"See the ",[767,1508,1510],{"href":1509},"\u002Fblog\u002Fsession-caching-servicenow-script-includes-before-query-business-rules","session caching guide",[19,1512,1514],{"id":1513},"common-mistakes","Common mistakes",[37,1516,1517,1523,1532,1543,1549],{},[40,1518,1519,1522],{},[27,1520,1521],{},"Name mismatch."," If the record name differs from the class or function name, callers get \"is not defined\" errors. Rename both together.",[40,1524,1525,1528,1529,1531],{},[27,1526,1527],{},"Client callable on a utility class."," Selecting the checkbox on a class that does not extend ",[32,1530,1085],{}," does not make it work with GlideAjax. It only marks code as browser-facing that was never reviewed for that.",[40,1533,1534,1537,1538,1542],{},[27,1535,1536],{},"Business logic in business rules."," Long scripts in ",[767,1539,1541],{"href":1540},"\u002Fblog\u002Fservicenow-business-rule-best-practices","business rules"," are hard to test and reuse. Move the logic into a Script Include and keep the rule to a single call.",[40,1544,1545,1548],{},[27,1546,1547],{},"Global state."," Each transaction gets a fresh instance of your class. Do not expect a variable set in one transaction to be there in the next; use the session or a table for that.",[40,1550,1551,1554,1555,1558],{},[27,1552,1553],{},"Unbounded queries."," A utility method that loops over every record on a large table will be slow everywhere it is reused. Add ",[32,1556,1557],{},"setLimit()"," or a tighter query.",[19,1560,1562],{"id":1561},"checklist-before-you-save","Checklist before you save",[37,1564,1567,1576,1585,1594,1602,1608,1614],{"className":1565},[1566],"contains-task-list",[40,1568,1571,1575],{"className":1569},[1570],"task-list-item",[1572,1573],"input",{"disabled":428,"type":1574},"checkbox"," The record name matches the class or function name.",[40,1577,1579,1581,1582,1584],{"className":1578},[1570],[1572,1580],{"disabled":428,"type":1574}," The ",[32,1583,747],{}," property matches the class name.",[40,1586,1588,1590,1591,1593],{"className":1587},[1570],[1572,1589],{"disabled":428,"type":1574}," ",[27,1592,60],{}," is as narrow as possible.",[40,1595,1597,1599,1600,338],{"className":1596},[1570],[1572,1598],{"disabled":428,"type":1574}," Client callable is selected only on classes that extend ",[32,1601,1085],{},[40,1603,1605,1607],{"className":1604},[1570],[1572,1606],{"disabled":428,"type":1574}," Client-callable methods validate every parameter and check roles.",[40,1609,1611,1613],{"className":1610},[1570],[1572,1612],{"disabled":428,"type":1574}," Server-only logic lives in a class that is not client callable.",[40,1615,1617,1619],{"className":1616},[1570],[1572,1618],{"disabled":428,"type":1574}," Queries have limits and use indexed fields where possible.",[12,1621,1622,1623,1626],{},"Start with the class-based pattern for anything more than one function. It is easy to extend later, easy to test from ",[27,1624,1625],{},"Scripts - Background",", and it keeps business rules, flows and Ajax classes small.",[1628,1629,1630],"style",{},"html pre.shiki code .sCydW, html code.shiki .sCydW{--shiki-default:#D73A49;--shiki-dark:#D73A49}html pre.shiki code .se37E, html code.shiki .se37E{--shiki-default:#6F42C1;--shiki-dark:#6F42C1}html pre.shiki code .sKWpL, html code.shiki .sKWpL{--shiki-default:#24292E;--shiki-dark:#24292E}html pre.shiki code .sj_tP, html code.shiki .sj_tP{--shiki-default:#E36209;--shiki-dark:#E36209}html pre.shiki code .sOTlB, html code.shiki .sOTlB{--shiki-default:#032F62;--shiki-dark:#032F62}html pre.shiki code .sMN4m, html code.shiki .sMN4m{--shiki-default:#005CC5;--shiki-dark:#005CC5}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);}",{"title":104,"searchDepth":133,"depth":133,"links":1632},[1633,1634,1635,1636,1637,1638,1639,1640,1641],{"id":21,"depth":133,"text":22},{"id":87,"depth":133,"text":88},{"id":330,"depth":133,"text":331},{"id":784,"depth":133,"text":785},{"id":1078,"depth":133,"text":1079},{"id":1386,"depth":133,"text":1387},{"id":1417,"depth":133,"text":1418},{"id":1513,"depth":133,"text":1514},{"id":1561,"depth":133,"text":1562},"2026-12-04","The main ServiceNow Script Include patterns: classless on-demand functions, class-based utilities, extended classes and client-callable GlideAjax classes.",null,"md",[1647,1650,1653,1656],{"question":1648,"answer":1649},"What is a classless or on-demand Script Include?","It is a Script Include that defines a single function instead of a class. The function name must match the Script Include name. Other server-side scripts call it directly by name, without the new keyword.",{"question":1651,"answer":1652},"Can a classless Script Include be called from a client script?","No. GlideAjax needs a class that extends AbstractAjaxProcessor and has the client callable (Glide AJAX enabled) option selected. Wrap the logic in a client-callable class if the browser needs it.",{"question":1654,"answer":1655},"How do I call a Script Include from another application scope?","Set Accessible from to All application scopes on the Script Include, then call it with its scope prefix, for example new x_acme_app.AssetUtils() or new global.MyUtil(). Cross-scope access rules may also need to allow the call.",{"question":1657,"answer":1658},"Should a client-callable Script Include also hold server-only logic?","No. Every public method on a client-callable class can be reached from the browser. Keep server-only logic in a separate class and have the GlideAjax class call it after checking input and roles.",{},"\u002Fblog\u002Fservicenow-script-include-patterns",{"title":5,"description":1643},"\u002Fsocial-card.png","blog\u002Fservicenow-script-include-patterns",[1665,1666,1667],"script includes","scripting","glideajax","P9hWkOXMNcDbfG6op0I0lHjWyrMchYbq1gXLc9Nmq7A",[1670,1676,1683],{"path":1368,"title":1671,"headline":1671,"description":1672,"date":1673,"tags":1674},"ServiceNow GlideAjax: Call a Script Include from a Client Script","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.","2026-11-25",[1667,1675,1665,1666],"client scripts",{"path":1677,"title":1678,"headline":1678,"description":1679,"date":1680,"tags":1681},"\u002Fblog\u002Fservicenow-client-script-best-practices","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",[1675,1666,1682],"forms",{"path":1684,"title":1685,"headline":1686,"description":1687,"date":1688,"tags":1689},"\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",[1690,1691,1692,1693,1694,1695,1696,1697],"ServiceNow","Scripts","Client Scripts","Business Rules","Script Includes","Development","Code Library","Best Practices",1796346314866]