es5-compliance

$npx mdskill add serac-labs/serac/es5-compliance

Convert ES6+ code to ES5 for ServiceNow compatibility

  • Fix syntax errors caused by unsupported ES6+ features in ServiceNow scripts
  • Uses Snow-Code tools and ServiceNow APIs for execution and conversion
  • Identifies modern JavaScript syntax and replaces it with ES5 equivalents
  • Provides converted code output for immediate use in ServiceNow environments

SKILL.md

.github/skills/es5-complianceView on GitHub ↗
---
name: es5-compliance
description: Enforce ES5-only syntax for ServiceNow server-side scripts (Rhino engine) — convert const/let, arrow functions, template literals, destructuring, for-of, and async/await to ES5 equivalents.
license: Apache-2.0
compatibility: Designed for Snow-Code and ServiceNow development
metadata:
  author: serac
  version: "1.0.0"
  category: servicenow
tools:
  - snow_execute_script_with_output
  - snow_convert_es6_to_es5
---

# ES5 Compliance for ServiceNow

ServiceNow runs on Mozilla Rhino engine which only supports ES5 JavaScript (2009 standard). All server-side scripts MUST use ES5 syntax.

## Forbidden Syntax (WILL CAUSE SyntaxError)

| ES6+ Syntax           | ES5 Alternative                        |
| --------------------- | -------------------------------------- |
| `const x = 5`         | `var x = 5`                            |
| `let items = []`      | `var items = []`                       |
| `() => {}`            | `function() {}`                        |
| `` `Hello ${name}` `` | `'Hello ' + name`                      |
| `for (x of arr)`      | `for (var i = 0; i < arr.length; i++)` |
| `{a, b} = obj`        | `var a = obj.a; var b = obj.b;`        |
| `[a, b] = arr`        | `var a = arr[0]; var b = arr[1];`      |
| `...spread`           | Use `Array.prototype.slice.call()`     |
| `class MyClass {}`    | Use constructor functions              |
| `async/await`         | Use GlideRecord callbacks              |
| `Promise`             | Use GlideRecord with callbacks         |

## Common Patterns

### Variable Declarations

```javascript
// WRONG - ES6
const MAX_RETRIES = 3
let currentUser = gs.getUser()

// CORRECT - ES5
var MAX_RETRIES = 3
var currentUser = gs.getUser()
```

### Functions

```javascript
// WRONG - Arrow functions
var active = incidents.filter((inc) => inc.active)
var process = () => {
  return "done"
}

// CORRECT - ES5 functions
var active = []
for (var i = 0; i < incidents.length; i++) {
  if (incidents[i].active) {
    active.push(incidents[i])
  }
}
var process = function () {
  return "done"
}
```

### String Concatenation

```javascript
// WRONG - Template literals
var message = `Incident ${number} assigned to ${user}`

// CORRECT - String concatenation
var message = "Incident " + number + " assigned to " + user
```

### Loops

```javascript
// WRONG - for...of
for (var item of items) {
  gs.info(item)
}

// CORRECT - Traditional for loop
for (var i = 0; i < items.length; i++) {
  gs.info(items[i])
}
```

### Default Parameters

```javascript
// WRONG - Default parameters
function process(incident, priority = 3) {
  // ...
}

// CORRECT - Manual defaults
function process(incident, priority) {
  if (typeof priority === "undefined") {
    priority = 3
  }
  // ...
}
```

## Automatic Validation

Before deploying any server-side script:

1. Check for `const`/`let` declarations - convert to `var`
2. Check for arrow functions `=>` - convert to `function()`
3. Check for template literals `` ` `` - convert to string concatenation
4. Check for destructuring `{a, b}` - convert to explicit property access
5. Check for `for...of` loops - convert to index-based loops

## Exception: Client Scripts

Client-side scripts (Client Scripts, UI Policies) run in the browser and MAY support ES6+ depending on user's browser. However, for maximum compatibility, ES5 is still recommended.

More from serac-labs/serac

SkillDescription
acl-securityCreate and debug ServiceNow ACLs (record, field, REST, script-include). Covers role/condition/script patterns, evaluation order, field-level visibility, and impersonation testing for row- and field-level security.
agent-workspaceBuild ServiceNow Agent Workspace configurations — workspaces, lists, forms, contextual side panels, Agent Assist similar-record finders, and workspace-specific UI actions on sys_aw_* tables.
approval-workflowsConfigure ServiceNow approval rules and sysapproval_approver records — manager/group/script approvers, multi-level routing, delegation via sys_user_delegate, and parent-record state rollup.
asset-managementManage ServiceNow hardware assets, software licenses, and lifecycle states on alm_hardware/alm_license — license allocation, CMDB-to-asset linking, warranty tracking, inventory aggregation (HAM/SAM).
atf-testingBuild ServiceNow Automated Test Framework tests and suites — impersonation, form steps, assertions, server-side script steps, test parameters, and execution via snow_create_atf_test / snow_execute_atf_test.
blast-radiusTrace ServiceNow configuration dependencies — what artifacts touch a given field, what calls a script include, table/app-level config inventory. Use before deletes, renames, or refactors.
bun-file-ioUse this when you are working on file operations like reading, writing, scanning, or deleting files. It summarizes the preferred file APIs and patterns used in this repo. It also notes when to use filesystem helpers for directories.
business-rule-patternsWrite ServiceNow business rules (before/after/async/display) — current vs previous, changesTo/changesFrom, recursion avoidance, setAbortAction, and async dispatch for heavy work.
catalog-itemsBuild ServiceNow Service Catalog items, variables, variable sets, catalog client scripts, record producers, and order guides with reference qualifiers and dynamic pricing.
client-scriptsWrite ServiceNow client scripts (onLoad/onChange/onSubmit/onCellEdit) using g_form, g_user, GlideAjax, field visibility/mandatory toggles, and validation with debounced server calls.