[{"data":1,"prerenderedAt":1386},["ShallowReactive",2],{"page-\u002Fblog\u002Fservicenow-client-script-best-practices\u002F":3,"related-\u002Fblog\u002Fservicenow-client-script-best-practices\u002F":1351},{"id":4,"title":5,"author":6,"authorUrl":7,"body":8,"date":1324,"dateUpdated":1324,"description":1325,"dqid":1324,"excerpt":1326,"extension":1327,"faq":1328,"headline":5,"meta":1341,"navigation":530,"path":1342,"seo":1343,"socialImage":1344,"stem":1345,"tags":1346,"__hash__":1350},"content\u002Fblog\u002Fservicenow-client-script-best-practices.md","ServiceNow Client Script Best Practices: onLoad, onChange, onSubmit","Gabriel Fordan","https:\u002F\u002Fsn-tricks.com\u002Fabout",{"type":9,"value":10,"toc":1314},"minimark",[11,33,36,41,52,118,129,133,136,152,155,273,328,337,341,344,424,427,430,454,462,466,473,480,696,703,915,918,937,949,956,960,967,970,1116,1123,1130,1138,1142,1199,1203,1256,1259,1263,1298,1310],[12,13,14,15,19,20,23,24,27,28,32],"p",{},"ServiceNow client script best practices come down to three rules: run as little code in the browser as possible, never block the form waiting on the server, and pick the script type that matches the moment you need. Use ",[16,17,18],"strong",{},"onLoad"," to prepare the form, ",[16,21,22],{},"onChange"," to react to one field, and ",[16,25,26],{},"onSubmit"," only for fast, final validation. Get data from the server with ",[29,30,31],"code",{},"g_scratchpad"," or asynchronous GlideAjax, not client-side GlideRecord, and use a UI policy when all you need is mandatory, read-only or visible logic.",[12,34,35],{},"The rest of this guide explains each rule with examples you can try on a development instance.",[37,38,40],"h2",{"id":39},"the-four-client-script-types","The four client script types",[12,42,43,44,47,48,51],{},"Client scripts live in the ",[29,45,46],{},"sys_script_client"," table. Open them from ",[16,49,50],{},"System Definition > Client Scripts",". Each record has a table, a type, a UI type and a script.",[53,54,55,71],"table",{},[56,57,58],"thead",{},[59,60,61,65,68],"tr",{},[62,63,64],"th",{},"Type",[62,66,67],{},"Runs when",[62,69,70],{},"Typical use",[72,73,74,87,97,107],"tbody",{},[59,75,76,79,82],{},[77,78,18],"td",{},[77,80,81],{},"The form finishes loading",[77,83,84,85],{},"Set defaults, show messages, read ",[29,86,31],{},[59,88,89,91,94],{},[77,90,22],{},[77,92,93],{},"A chosen field changes, and once during load",[77,95,96],{},"Clear or set dependent fields, fetch related data",[59,98,99,101,104],{},[77,100,26],{},[77,102,103],{},"The user saves or submits",[77,105,106],{},"Last check before the record goes to the server",[59,108,109,112,115],{},[77,110,111],{},"onCellEdit",[77,113,114],{},"A cell is edited in a list",[77,116,117],{},"Block or confirm list edits",[12,119,120,121,124,125,128],{},"Set the ",[16,122,123],{},"UI Type"," field deliberately. Desktop scripts do not run in Service Portal or mobile. If the form is used in the portal too, choose ",[16,126,127],{},"All"," and test it there.",[37,130,132],{"id":131},"onload-keep-it-small","onLoad: keep it small",[12,134,135],{},"Every onLoad script adds time before the user can work. A form with ten onLoad scripts that each call the server feels slow even if each call is quick.",[137,138,139,143,149],"ul",{},[140,141,142],"li",{},"Combine related onLoad logic for one table into a single script where it makes sense.",[140,144,145,146,148],{},"Read server data from ",[29,147,31],{},", filled by a display business rule, instead of querying from the browser.",[140,150,151],{},"Do not hide or make fields mandatory in onLoad if a UI policy can do it.",[12,153,154],{},"A display business rule runs on the server before the form is sent, so the data arrives with the form:",[156,157,162],"pre",{"className":158,"code":159,"language":160,"meta":161,"style":161},"language-javascript shiki shiki-themes github-light github-light","\u002F\u002F Display business rule on incident (server side)\n(function executeRule(current, previous) {\n    var caller = current.caller_id.getRefRecord();\n    g_scratchpad.callerIsVip = caller.isValidRecord() && caller.getValue('vip') == '1';\n})(current, previous);\n","javascript","",[29,163,164,173,203,224,267],{"__ignoreMap":161},[165,166,169],"span",{"class":167,"line":168},"line",1,[165,170,172],{"class":171},"sJ8bj","\u002F\u002F Display business rule on incident (server side)\n",[165,174,176,180,184,188,190,194,197,200],{"class":167,"line":175},2,[165,177,179],{"class":178},"sKWpL","(",[165,181,183],{"class":182},"sCydW","function",[165,185,187],{"class":186},"se37E"," executeRule",[165,189,179],{"class":178},[165,191,193],{"class":192},"sj_tP","current",[165,195,196],{"class":178},", ",[165,198,199],{"class":192},"previous",[165,201,202],{"class":178},") {\n",[165,204,206,209,212,215,218,221],{"class":167,"line":205},3,[165,207,208],{"class":182},"    var",[165,210,211],{"class":178}," caller ",[165,213,214],{"class":182},"=",[165,216,217],{"class":178}," current.caller_id.",[165,219,220],{"class":186},"getRefRecord",[165,222,223],{"class":178},"();\n",[165,225,227,230,232,235,238,241,244,246,249,251,255,258,261,264],{"class":167,"line":226},4,[165,228,229],{"class":178},"    g_scratchpad.callerIsVip ",[165,231,214],{"class":182},[165,233,234],{"class":178}," caller.",[165,236,237],{"class":186},"isValidRecord",[165,239,240],{"class":178},"() ",[165,242,243],{"class":182},"&&",[165,245,234],{"class":178},[165,247,248],{"class":186},"getValue",[165,250,179],{"class":178},[165,252,254],{"class":253},"sOTlB","'vip'",[165,256,257],{"class":178},") ",[165,259,260],{"class":182},"==",[165,262,263],{"class":253}," '1'",[165,265,266],{"class":178},";\n",[165,268,270],{"class":167,"line":269},5,[165,271,272],{"class":178},"})(current, previous);\n",[156,274,276],{"className":158,"code":275,"language":160,"meta":161,"style":161},"\u002F\u002F onLoad client script on incident\nfunction onLoad() {\n    if (g_scratchpad.callerIsVip) {\n        g_form.addInfoMessage('This caller is a VIP. Follow the VIP handling process.');\n    }\n}\n",[29,277,278,283,293,301,317,322],{"__ignoreMap":161},[165,279,280],{"class":167,"line":168},[165,281,282],{"class":171},"\u002F\u002F onLoad client script on incident\n",[165,284,285,287,290],{"class":167,"line":175},[165,286,183],{"class":182},[165,288,289],{"class":186}," onLoad",[165,291,292],{"class":178},"() {\n",[165,294,295,298],{"class":167,"line":205},[165,296,297],{"class":182},"    if",[165,299,300],{"class":178}," (g_scratchpad.callerIsVip) {\n",[165,302,303,306,309,311,314],{"class":167,"line":226},[165,304,305],{"class":178},"        g_form.",[165,307,308],{"class":186},"addInfoMessage",[165,310,179],{"class":178},[165,312,313],{"class":253},"'This caller is a VIP. Follow the VIP handling process.'",[165,315,316],{"class":178},");\n",[165,318,319],{"class":167,"line":269},[165,320,321],{"class":178},"    }\n",[165,323,325],{"class":167,"line":324},6,[165,326,327],{"class":178},"}\n",[12,329,330,331,336],{},"No extra round trip, and the client script stays a few lines long. The ",[332,333,335],"a",{"href":334},"\u002Fblog\u002Fservicenow-business-rule-best-practices","business rule best practices guide"," covers display rules in more depth.",[37,338,340],{"id":339},"onchange-respect-isloading","onChange: respect isLoading",[12,342,343],{},"ServiceNow calls every onChange script once while the form loads. That is why the default template starts with a guard:",[156,345,347],{"className":158,"code":346,"language":160,"meta":161,"style":161},"function onChange(control, oldValue, newValue, isLoading, isTemplate) {\n    if (isLoading || newValue === '') {\n        return;\n    }\n    \u002F\u002F logic for a real user change\n}\n",[29,348,349,383,404,411,415,420],{"__ignoreMap":161},[165,350,351,353,356,358,361,363,366,368,371,373,376,378,381],{"class":167,"line":168},[165,352,183],{"class":182},[165,354,355],{"class":186}," onChange",[165,357,179],{"class":178},[165,359,360],{"class":192},"control",[165,362,196],{"class":178},[165,364,365],{"class":192},"oldValue",[165,367,196],{"class":178},[165,369,370],{"class":192},"newValue",[165,372,196],{"class":178},[165,374,375],{"class":192},"isLoading",[165,377,196],{"class":178},[165,379,380],{"class":192},"isTemplate",[165,382,202],{"class":178},[165,384,385,387,390,393,396,399,402],{"class":167,"line":175},[165,386,297],{"class":182},[165,388,389],{"class":178}," (isLoading ",[165,391,392],{"class":182},"||",[165,394,395],{"class":178}," newValue ",[165,397,398],{"class":182},"===",[165,400,401],{"class":253}," ''",[165,403,202],{"class":178},[165,405,406,409],{"class":167,"line":205},[165,407,408],{"class":182},"        return",[165,410,266],{"class":178},[165,412,413],{"class":167,"line":226},[165,414,321],{"class":178},[165,416,417],{"class":167,"line":269},[165,418,419],{"class":171},"    \u002F\u002F logic for a real user change\n",[165,421,422],{"class":167,"line":324},[165,423,327],{"class":178},[12,425,426],{},"Keep the guard unless you want the logic to run on load. Removing it is a common cause of fields being cleared or overwritten as soon as a record opens.",[12,428,429],{},"Other onChange rules:",[137,431,432,442,448],{},[140,433,434,437,438,441],{},[16,435,436],{},"Compare values before acting."," If ",[29,439,440],{},"newValue === oldValue",", there is nothing to do.",[140,443,444,447],{},[16,445,446],{},"Avoid chains."," Setting field B from an onChange on field A fires B's onChange too. Two scripts that set each other's fields can loop. Keep dependencies one way.",[140,449,450,453],{},[16,451,452],{},"Handle the empty case on purpose."," The template returns when the field is cleared. If clearing field A should also clear field B, handle that before the return.",[12,455,456,457,461],{},"A good small example is the library script that ",[332,458,460],{"href":459},"\u002Fscripts\u002Fclear-assignee-on-assignment-group-change","clears the assignee when the assignment group changes",".",[37,463,465],{"id":464},"call-the-server-with-asynchronous-glideajax","Call the server with asynchronous GlideAjax",[12,467,468,469,472],{},"When an onChange needs server data, use GlideAjax with ",[29,470,471],{},"getXMLAnswer"," and a callback. The form stays responsive while the request runs.",[12,474,475,476,479],{},"The server side is a client-callable script include. Select ",[16,477,478],{},"Client callable"," on the script include record:",[156,481,483],{"className":158,"code":482,"language":160,"meta":161,"style":161},"var IncidentClientUtils = Class.create();\nIncidentClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {\n\n    isVipUser: function() {\n        var userId = this.getParameter('sysparm_user_id');\n        var user = new GlideRecordSecure('sys_user');\n        if (!userId || !user.get(userId)) {\n            return 'false';\n        }\n        return user.getValue('vip') == '1' ? 'true' : 'false';\n    },\n\n    type: 'IncidentClientUtils'\n});\n",[29,484,485,503,526,532,544,569,591,620,631,637,670,676,681,690],{"__ignoreMap":161},[165,486,487,490,493,495,498,501],{"class":167,"line":168},[165,488,489],{"class":182},"var",[165,491,492],{"class":178}," IncidentClientUtils ",[165,494,214],{"class":182},[165,496,497],{"class":178}," Class.",[165,499,500],{"class":186},"create",[165,502,223],{"class":178},[165,504,505,509,511,514,517,520,523],{"class":167,"line":175},[165,506,508],{"class":507},"sMN4m","IncidentClientUtils",[165,510,461],{"class":178},[165,512,513],{"class":507},"prototype",[165,515,516],{"class":182}," =",[165,518,519],{"class":178}," Object.",[165,521,522],{"class":186},"extendsObject",[165,524,525],{"class":178},"(AbstractAjaxProcessor, {\n",[165,527,528],{"class":167,"line":205},[165,529,531],{"emptyLinePlaceholder":530},true,"\n",[165,533,534,537,540,542],{"class":167,"line":226},[165,535,536],{"class":186},"    isVipUser",[165,538,539],{"class":178},": ",[165,541,183],{"class":182},[165,543,292],{"class":178},[165,545,546,549,552,554,557,559,562,564,567],{"class":167,"line":269},[165,547,548],{"class":182},"        var",[165,550,551],{"class":178}," userId ",[165,553,214],{"class":182},[165,555,556],{"class":507}," this",[165,558,461],{"class":178},[165,560,561],{"class":186},"getParameter",[165,563,179],{"class":178},[165,565,566],{"class":253},"'sysparm_user_id'",[165,568,316],{"class":178},[165,570,571,573,576,578,581,584,586,589],{"class":167,"line":324},[165,572,548],{"class":182},[165,574,575],{"class":178}," user ",[165,577,214],{"class":182},[165,579,580],{"class":182}," new",[165,582,583],{"class":186}," GlideRecordSecure",[165,585,179],{"class":178},[165,587,588],{"class":253},"'sys_user'",[165,590,316],{"class":178},[165,592,594,597,600,603,606,608,611,614,617],{"class":167,"line":593},7,[165,595,596],{"class":182},"        if",[165,598,599],{"class":178}," (",[165,601,602],{"class":182},"!",[165,604,605],{"class":178},"userId ",[165,607,392],{"class":182},[165,609,610],{"class":182}," !",[165,612,613],{"class":178},"user.",[165,615,616],{"class":186},"get",[165,618,619],{"class":178},"(userId)) {\n",[165,621,623,626,629],{"class":167,"line":622},8,[165,624,625],{"class":182},"            return",[165,627,628],{"class":253}," 'false'",[165,630,266],{"class":178},[165,632,634],{"class":167,"line":633},9,[165,635,636],{"class":178},"        }\n",[165,638,640,642,645,647,649,651,653,655,657,660,663,666,668],{"class":167,"line":639},10,[165,641,408],{"class":182},[165,643,644],{"class":178}," user.",[165,646,248],{"class":186},[165,648,179],{"class":178},[165,650,254],{"class":253},[165,652,257],{"class":178},[165,654,260],{"class":182},[165,656,263],{"class":253},[165,658,659],{"class":182}," ?",[165,661,662],{"class":253}," 'true'",[165,664,665],{"class":182}," :",[165,667,628],{"class":253},[165,669,266],{"class":178},[165,671,673],{"class":167,"line":672},11,[165,674,675],{"class":178},"    },\n",[165,677,679],{"class":167,"line":678},12,[165,680,531],{"emptyLinePlaceholder":530},[165,682,684,687],{"class":167,"line":683},13,[165,685,686],{"class":178},"    type: ",[165,688,689],{"class":253},"'IncidentClientUtils'\n",[165,691,693],{"class":167,"line":692},14,[165,694,695],{"class":178},"});\n",[12,697,698,699,702],{},"The onChange script on the ",[29,700,701],{},"caller_id"," field calls it:",[156,704,706],{"className":158,"code":705,"language":160,"meta":161,"style":161},"function onChange(control, oldValue, newValue, isLoading, isTemplate) {\n    if (isLoading || newValue === '') {\n        g_form.hideFieldMsg('caller_id');\n        return;\n    }\n\n    var ga = new GlideAjax('IncidentClientUtils');\n    ga.addParam('sysparm_name', 'isVipUser');\n    ga.addParam('sysparm_user_id', newValue);\n    ga.getXMLAnswer(function(answer) {\n        g_form.hideFieldMsg('caller_id');\n        if (answer === 'true') {\n            g_form.showFieldMsg('caller_id', 'VIP caller', 'info');\n        }\n    });\n}\n",[29,707,708,736,752,766,772,776,780,801,821,834,851,863,876,900,904,910],{"__ignoreMap":161},[165,709,710,712,714,716,718,720,722,724,726,728,730,732,734],{"class":167,"line":168},[165,711,183],{"class":182},[165,713,355],{"class":186},[165,715,179],{"class":178},[165,717,360],{"class":192},[165,719,196],{"class":178},[165,721,365],{"class":192},[165,723,196],{"class":178},[165,725,370],{"class":192},[165,727,196],{"class":178},[165,729,375],{"class":192},[165,731,196],{"class":178},[165,733,380],{"class":192},[165,735,202],{"class":178},[165,737,738,740,742,744,746,748,750],{"class":167,"line":175},[165,739,297],{"class":182},[165,741,389],{"class":178},[165,743,392],{"class":182},[165,745,395],{"class":178},[165,747,398],{"class":182},[165,749,401],{"class":253},[165,751,202],{"class":178},[165,753,754,756,759,761,764],{"class":167,"line":205},[165,755,305],{"class":178},[165,757,758],{"class":186},"hideFieldMsg",[165,760,179],{"class":178},[165,762,763],{"class":253},"'caller_id'",[165,765,316],{"class":178},[165,767,768,770],{"class":167,"line":226},[165,769,408],{"class":182},[165,771,266],{"class":178},[165,773,774],{"class":167,"line":269},[165,775,321],{"class":178},[165,777,778],{"class":167,"line":324},[165,779,531],{"emptyLinePlaceholder":530},[165,781,782,784,787,789,791,794,796,799],{"class":167,"line":593},[165,783,208],{"class":182},[165,785,786],{"class":178}," ga ",[165,788,214],{"class":182},[165,790,580],{"class":182},[165,792,793],{"class":186}," GlideAjax",[165,795,179],{"class":178},[165,797,798],{"class":253},"'IncidentClientUtils'",[165,800,316],{"class":178},[165,802,803,806,809,811,814,816,819],{"class":167,"line":622},[165,804,805],{"class":178},"    ga.",[165,807,808],{"class":186},"addParam",[165,810,179],{"class":178},[165,812,813],{"class":253},"'sysparm_name'",[165,815,196],{"class":178},[165,817,818],{"class":253},"'isVipUser'",[165,820,316],{"class":178},[165,822,823,825,827,829,831],{"class":167,"line":633},[165,824,805],{"class":178},[165,826,808],{"class":186},[165,828,179],{"class":178},[165,830,566],{"class":253},[165,832,833],{"class":178},", newValue);\n",[165,835,836,838,840,842,844,846,849],{"class":167,"line":639},[165,837,805],{"class":178},[165,839,471],{"class":186},[165,841,179],{"class":178},[165,843,183],{"class":182},[165,845,179],{"class":178},[165,847,848],{"class":192},"answer",[165,850,202],{"class":178},[165,852,853,855,857,859,861],{"class":167,"line":672},[165,854,305],{"class":178},[165,856,758],{"class":186},[165,858,179],{"class":178},[165,860,763],{"class":253},[165,862,316],{"class":178},[165,864,865,867,870,872,874],{"class":167,"line":678},[165,866,596],{"class":182},[165,868,869],{"class":178}," (answer ",[165,871,398],{"class":182},[165,873,662],{"class":253},[165,875,202],{"class":178},[165,877,878,881,884,886,888,890,893,895,898],{"class":167,"line":683},[165,879,880],{"class":178},"            g_form.",[165,882,883],{"class":186},"showFieldMsg",[165,885,179],{"class":178},[165,887,763],{"class":253},[165,889,196],{"class":178},[165,891,892],{"class":253},"'VIP caller'",[165,894,196],{"class":178},[165,896,897],{"class":253},"'info'",[165,899,316],{"class":178},[165,901,902],{"class":167,"line":692},[165,903,636],{"class":178},[165,905,907],{"class":167,"line":906},15,[165,908,909],{"class":178},"    });\n",[165,911,913],{"class":167,"line":912},16,[165,914,327],{"class":178},[12,916,917],{},"Points worth copying:",[137,919,920,927,930],{},[140,921,922,923,926],{},"The script include uses ",[29,924,925],{},"GlideRecordSecure",", so the user only gets an answer for records they can read. Anything client callable can be called by any logged-in user with crafted parameters, so validate inputs and check access.",[140,928,929],{},"Return a small string, not a whole record.",[140,931,932,933,936],{},"One GlideAjax call can return several values as a JSON string. Parse it with ",[29,934,935],{},"JSON.parse(answer)"," in the callback.",[12,938,939,940,944,945,461],{},"The library has a ready ",[332,941,943],{"href":942},"\u002Fscripts\u002Fglideajax-server-call","GlideAjax server call"," and a matching ",[332,946,948],{"href":947},"\u002Fscripts\u002Fglideajax-response-handler","response handler script include",[12,950,951,952,955],{},"For a single field on a referenced record, ",[29,953,954],{},"g_form.getReference('caller_id', callback)"," also works asynchronously. Always pass the callback. Without one, the call is synchronous and the form waits for the server.",[37,957,959],{"id":958},"onsubmit-fast-and-synchronous","onSubmit: fast and synchronous",[12,961,962,963,966],{},"An onSubmit script stops the save by returning ",[29,964,965],{},"false",". It must decide right away, which creates a trap: an asynchronous GlideAjax callback runs after the function has already returned, so it cannot cancel the submit.",[12,968,969],{},"Work around it by doing the server lookup earlier. Fetch what you need in an onChange, keep the result on the form or in a client variable, and let onSubmit check it locally:",[156,971,973],{"className":158,"code":972,"language":160,"meta":161,"style":161},"function onSubmit() {\n    var state = g_form.getValue('state');\n    var notes = g_form.getValue('close_notes');\n\n    if (state === '6' && notes.trim() === '') {\n        g_form.clearMessages();\n        g_form.showFieldMsg('close_notes', 'Close notes are required to resolve.', 'error');\n        return false;\n    }\n    return true;\n}\n",[29,974,975,984,1005,1025,1029,1058,1067,1089,1098,1102,1112],{"__ignoreMap":161},[165,976,977,979,982],{"class":167,"line":168},[165,978,183],{"class":182},[165,980,981],{"class":186}," onSubmit",[165,983,292],{"class":178},[165,985,986,988,991,993,996,998,1000,1003],{"class":167,"line":175},[165,987,208],{"class":182},[165,989,990],{"class":178}," state ",[165,992,214],{"class":182},[165,994,995],{"class":178}," g_form.",[165,997,248],{"class":186},[165,999,179],{"class":178},[165,1001,1002],{"class":253},"'state'",[165,1004,316],{"class":178},[165,1006,1007,1009,1012,1014,1016,1018,1020,1023],{"class":167,"line":205},[165,1008,208],{"class":182},[165,1010,1011],{"class":178}," notes ",[165,1013,214],{"class":182},[165,1015,995],{"class":178},[165,1017,248],{"class":186},[165,1019,179],{"class":178},[165,1021,1022],{"class":253},"'close_notes'",[165,1024,316],{"class":178},[165,1026,1027],{"class":167,"line":226},[165,1028,531],{"emptyLinePlaceholder":530},[165,1030,1031,1033,1036,1038,1041,1044,1047,1050,1052,1054,1056],{"class":167,"line":269},[165,1032,297],{"class":182},[165,1034,1035],{"class":178}," (state ",[165,1037,398],{"class":182},[165,1039,1040],{"class":253}," '6'",[165,1042,1043],{"class":182}," &&",[165,1045,1046],{"class":178}," notes.",[165,1048,1049],{"class":186},"trim",[165,1051,240],{"class":178},[165,1053,398],{"class":182},[165,1055,401],{"class":253},[165,1057,202],{"class":178},[165,1059,1060,1062,1065],{"class":167,"line":324},[165,1061,305],{"class":178},[165,1063,1064],{"class":186},"clearMessages",[165,1066,223],{"class":178},[165,1068,1069,1071,1073,1075,1077,1079,1082,1084,1087],{"class":167,"line":593},[165,1070,305],{"class":178},[165,1072,883],{"class":186},[165,1074,179],{"class":178},[165,1076,1022],{"class":253},[165,1078,196],{"class":178},[165,1080,1081],{"class":253},"'Close notes are required to resolve.'",[165,1083,196],{"class":178},[165,1085,1086],{"class":253},"'error'",[165,1088,316],{"class":178},[165,1090,1091,1093,1096],{"class":167,"line":622},[165,1092,408],{"class":182},[165,1094,1095],{"class":507}," false",[165,1097,266],{"class":178},[165,1099,1100],{"class":167,"line":633},[165,1101,321],{"class":178},[165,1103,1104,1107,1110],{"class":167,"line":639},[165,1105,1106],{"class":182},"    return",[165,1108,1109],{"class":507}," true",[165,1111,266],{"class":178},[165,1113,1114],{"class":167,"line":672},[165,1115,327],{"class":178},[12,1117,1118,1119,1122],{},"The state value ",[29,1120,1121],{},"6"," is Resolved on the base incident table. Check the choice list on your instance before reusing it.",[12,1124,1125,1126,1129],{},"Avoid synchronous GlideAjax (",[29,1127,1128],{},"getXMLWait","). It freezes the browser while it waits and is not supported in Service Portal.",[12,1131,1132,1133,1137],{},"Remember that onSubmit is a user experience check, not a security control. List edits, imports, REST calls and scripts skip client scripts entirely. Enforce the rule on the server with a business rule or data policy as well. The library includes an example that ",[332,1134,1136],{"href":1135},"\u002Fscripts\u002Fprevent-form-submission","prevents form submission"," when validation fails.",[37,1139,1141],{"id":1140},"keep-scripts-isolated-and-supported","Keep scripts isolated and supported",[137,1143,1144,1157,1179,1189],{},[140,1145,1146,1149,1150,196,1153,1156],{},[16,1147,1148],{},"Leave Isolate script selected."," New client scripts have it selected by default. It stops the script from reaching ",[29,1151,1152],{},"window",[29,1154,1155],{},"document"," or jQuery directly. DOM manipulation breaks on upgrades and in other interfaces.",[140,1158,1159,1162,1163,196,1166,196,1169,196,1172,1175,1176,1178],{},[16,1160,1161],{},"Use g_form, g_user and g_scratchpad only."," Methods like ",[29,1164,1165],{},"g_form.setValue",[29,1167,1168],{},"setMandatory",[29,1170,1171],{},"setDisplay",[29,1173,1174],{},"setReadOnly"," and ",[29,1177,883],{}," are supported across UIs.",[140,1180,1181,1184,1185,1188],{},[16,1182,1183],{},"Pass display values to reference fields."," ",[29,1186,1187],{},"g_form.setValue('assigned_to', sysId, displayName)"," saves a server round trip to look up the display value.",[140,1190,1191,1198],{},[16,1192,1193,1194,1197],{},"Do not trust ",[29,1195,1196],{},"g_user.hasRole"," for security."," It is fine for showing or hiding help text. Access must be enforced by ACLs.",[37,1200,1202],{"id":1201},"client-script-or-ui-policy","Client script or UI policy?",[53,1204,1205,1215],{},[56,1206,1207],{},[59,1208,1209,1212],{},[62,1210,1211],{},"Need",[62,1213,1214],{},"Use",[72,1216,1217,1225,1233,1240,1248],{},[59,1218,1219,1222],{},[77,1220,1221],{},"Make a field mandatory, read-only or hidden on a condition",[77,1223,1224],{},"UI policy",[59,1226,1227,1230],{},[77,1228,1229],{},"Set or clear a value",[77,1231,1232],{},"Client script",[59,1234,1235,1238],{},[77,1236,1237],{},"Show a message or confirm with the user",[77,1239,1232],{},[59,1241,1242,1245],{},[77,1243,1244],{},"Call the server",[77,1246,1247],{},"Client script with GlideAjax",[59,1249,1250,1253],{},[77,1251,1252],{},"Enforce a rule for every channel",[77,1254,1255],{},"Data policy or business rule",[12,1257,1258],{},"UI policies reverse themselves when the condition becomes false. A client script that hides a field must also show it again, which is easy to forget.",[37,1260,1262],{"id":1261},"a-short-checklist","A short checklist",[137,1264,1265,1268,1273,1276,1281,1286,1289,1292,1295],{},[140,1266,1267],{},"Correct type, table and UI type for every script",[140,1269,1270,1272],{},[29,1271,375],{}," guard kept in onChange scripts",[140,1274,1275],{},"No client-side GlideRecord and no synchronous GlideAjax",[140,1277,1278,1279],{},"Server data on load comes from ",[29,1280,31],{},[140,1282,1283,1284],{},"GlideAjax script includes validate input and use ",[29,1285,925],{},[140,1287,1288],{},"onSubmit checks are local, quick and backed by a server rule",[140,1290,1291],{},"Isolate script left selected, no DOM access",[140,1293,1294],{},"UI policies used for simple mandatory, read-only and visibility rules",[140,1296,1297],{},"Tested in every interface listed in the UI type",[12,1299,1300,1301,1305,1306,461],{},"For the server-side habits that pair with these rules, read ",[332,1302,1304],{"href":1303},"\u002Fblog\u002Fmastering-servicenow-scripting","ServiceNow coding best practices",". To keep forms working after each change, add a few form tests to your ",[332,1307,1309],{"href":1308},"\u002Fblog\u002Fservicenow-atf-regression-testing-best-practices","ATF regression suite",[1311,1312,1313],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sKWpL, html code.shiki .sKWpL{--shiki-default:#24292E;--shiki-dark:#24292E}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 .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 .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 .sMN4m, html code.shiki .sMN4m{--shiki-default:#005CC5;--shiki-dark:#005CC5}",{"title":161,"searchDepth":175,"depth":175,"links":1315},[1316,1317,1318,1319,1320,1321,1322,1323],{"id":39,"depth":175,"text":40},{"id":131,"depth":175,"text":132},{"id":339,"depth":175,"text":340},{"id":464,"depth":175,"text":465},{"id":958,"depth":175,"text":959},{"id":1140,"depth":175,"text":1141},{"id":1201,"depth":175,"text":1202},{"id":1261,"depth":175,"text":1262},"2026-11-20","ServiceNow client script best practices for onLoad, onChange and onSubmit: when to use each type, how to avoid slow forms, and safe GlideAjax patterns.",null,"md",[1329,1332,1335,1338],{"question":1330,"answer":1331},"What are the types of client scripts in ServiceNow?","There are four: onLoad runs when the form loads, onChange runs when a specific field changes (and also once during form load), onSubmit runs when the form is saved or submitted, and onCellEdit runs when a value is edited in a list. Client scripts are stored in the sys_script_client table.",{"question":1333,"answer":1334},"Why does my onChange client script run when the form loads?","ServiceNow calls onChange scripts during form load with the isLoading parameter set to true. The default template returns early when isLoading is true or newValue is empty. Keep that guard unless you deliberately want the logic to run on load.",{"question":1336,"answer":1337},"Should I use GlideRecord in a client script?","No. Client-side GlideRecord makes a synchronous round trip per query, returns every field, and is not supported in Service Portal. Use g_scratchpad from a display business rule for data needed on load, or GlideAjax with an asynchronous callback for data needed after a change.",{"question":1339,"answer":1340},"When should I use a UI policy instead of a client script?","Use a UI policy when you only need to make fields mandatory, read-only or hidden based on conditions. UI policies need no code and reverse automatically when the condition becomes false. Use a client script when you need to set values, call the server, show messages or apply logic a condition builder cannot express.",{},"\u002Fblog\u002Fservicenow-client-script-best-practices",{"title":5,"description":1325},"\u002Fsocial-card.png","blog\u002Fservicenow-client-script-best-practices",[1347,1348,1349],"client scripts","scripting","forms","amYpMqxJ1BYE9UmtbC-fAKlIa-IQKMM-p-f8e4zVIkE",[1352,1367,1375],{"path":1353,"title":1354,"headline":1355,"description":1356,"date":1357,"tags":1358},"\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",[1359,1360,1361,1362,1363,1364,1365,1366],"ServiceNow","Scripts","Client Scripts","Business Rules","Script Includes","Development","Code Library","Best Practices",{"path":1368,"title":1369,"headline":1369,"description":1370,"date":1371,"tags":1372},"\u002Fblog\u002Fservicenow-scripted-rest-api-example","Scripted REST API ServiceNow Example: Build One End to End","A worked Scripted REST API example in ServiceNow: create the API, add GET and POST resources, read parameters, return errors, secure it, and test with curl.","2026-11-16",[1373,1374,1348],"scripted rest api","integrations",{"path":1376,"title":1377,"headline":1378,"description":1379,"date":1380,"tags":1381},"\u002Fblog\u002Fsession-caching-servicenow-script-includes-before-query-business-rules","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",[1359,1382,1383,1384,1362,1385,1366],"Performance","Caching","Script Include","Optimization",1795144279647]