How to enable ES6 Support in Client Scripts (Any version)
Turns out you could've been writing ES6 Code all this time, but a typo is what's been preventing you from doing it.
ServiceNow have known about this for 4 years and have recorded it in PRB733616, but have deemed it "too risky to fix". So if you are okay with some minor customisation to enable this feature, feel free to proceed onwards.
2022 Update: Step 1 below has been fixed as of San Diego RTP.
Step 1: Update Validation Scripts
The following 2 records in your instance control the server-side validation of Script fields in ServiceNow.
https://[instancename].service-now.com/nav_to.do?uri=sys_script_validator.do?sys_id=279c99440a0a0a6f00872ceab0e759ed
https://[instancename].service-now.com/nav_to.do?uri=sys_script_validator.do?sys_id=2780cf700a0a0a6f0060332eba6ab860
Note: if those links dont work, here's the url:
Table: sys_script_validator
Filter: https://[instancename].service-now.com/sys_script_validator_list.do?sysparm_query=ui_type%3D0%5EGOTOinternal_type%3Dscript%5EORinternal_type%3Dscript_plain
Both of these validators run the same validation script but for the 2 different field types, Script and Script (Plain).
If you open them, you will see the following code:
function validate(value) {
if(g_form.getTableName() === 'sys_ui_script' || g_form.getTableName === 'sys_client_script')
return true;
var ajax = new GlideAjax('JSValidator');
ajax.addParam('sysparm_name','validate');
ajax.addParam('sysparm_js_expression',value);
ajax.getXMLWait();
var answer = ajax.getAnswer();
if (answer === null)
return true;
answer = getMessage("Could not save record because of a compile error: " + answer);
return answer;
}
If you look at line 2, there are 2 typos.
- sys_client_script is the incorrect table name for Client Script, the correct name is sys_script_client
- the second g_form.getTableName function is not invoked with parentheses
If you fix both of these typos, your fixed script should look like this:
function validate(value) {
if(g_form.getTableName() === 'sys_ui_script' || g_form.getTableName() === 'sys_script_client')
return true;
var ajax = new GlideAjax('JSValidator');
ajax.addParam('sysparm_name','validate');
ajax.addParam('sysparm_js_expression',value);
ajax.getXMLWait();
var answer = ajax.getAnswer();
if (answer === null)
return true;
answer = getMessage("Could not save record because of a compile error: " + answer);
return answer;
}
Save both records with the updated scripts.
Step 2: Write ES6 Client Scripts
Now you can go to any Client Script and start scripting in ES6, ES7, or any new version of JavaScript. If your browser supports it, it will run.
Note: You have to disable the Syntax Checker on the script field or it will give you errors.
Thats it, happy coding.
Bonus: Adding ES6 support for Service Portal widgets
You can also add support for saving ES6 code within Service Portal widgets by further updating the code to read as below. However, it will disable all server-side syntax checking for all Script fields on the SP Widget record, and it will not prevent the Widget Editor UI from showing Syntax Errors when you use patterns such as 'let', 'const', and arrow functions.
function validate(value) {
if(g_form.getTableName() === 'sys_ui_script' || g_form.getTableName() === 'sys_script_client' || g_form.getTableName() === 'sp_widget')
return true;
var ajax = new GlideAjax('JSValidator');
ajax.addParam('sysparm_name','validate');
ajax.addParam('sysparm_js_expression',value);
ajax.getXMLWait();
var answer = ajax.getAnswer();
if (answer === null)
return true;
answer = getMessage("Could not save record because of a compile error: " + answer);
return answer;
}
https://www.servicenow.com/community/developer-articles/how-to-enable-es6-support-in-client-scripts-any-version/ta-p/2324021