A custom Service Portal widget is built from an HTML template, a client script and a server script, with optional parts for instance options, Angular Providers and dependencies. You create one in the Widget Editor, either from scratch or by cloning, then test it on a page and trace faults with the diagnostic tools.
Learn the parts a widget is built from
| Component | Mandatory | Role |
|---|---|---|
| HTML template | Yes | Renders the view and binds client variables to markup |
| Client script | Yes | Maps server data to client objects and passes user input back |
| Server script | Yes | Sets the initial state and runs server-side queries |
| Link function | No | Manipulates the DOM directly |
| Option schema | No | Defines parameters an administrator sets per instance |
| Angular Providers | No | Shares context, persists state and injects reusable behavior |
| Dependencies | No | Loads external JavaScript or CSS libraries |
The server script relies on the ServiceNow API for record queries, creates and updates. The template and client script rely on AngularJS for data binding and gathering input. For the wider concept behind each part, see the widget explainer.
Follow the data from server to client
The server script runs first when a widget renders. Three global objects are available to it. The input object carries data sent from the client, so it is undefined on first load. The options object holds instance settings, and data starts empty and is sent to the client as JSON.
The client reads the result through c.data, where c is the widget controller. To send changes back, the client calls server.update(), which posts c.data to the server script as input and runs that script again. To replace the client’s options and data with the server’s response, call server.refresh().
Start from a new widget or a clone
To build from scratch, open the Widget Editor from Service Portal Configuration, choose Create a new widget and enter a name. The widget ID follows from the name. The widget is stored as an sp_widget record, and the editor lets you show and hide components while previewing. Tick Create test page if you want a page built around it.
Cloning is faster when a base widget already does much of the job. Base system widgets cannot be edited, so they keep receiving updates. A clone is treated as custom and stops receiving updates from its source, so you take on its maintenance. If the source uses an Angular ng-template, clone the template by hand and rename the reference. The header menu widget, for example, refers to a template called menuTemplate, and its clone needs a uniquely named copy.
Applications whose JavaScript mode is set to ECMAScript 2021 (ES12) run server scripts in ES12 by default, and applications on ES5 Standards or Compatibility mode can turn it on for an individual script.
Design instance options so administrators can configure the widget
Instance options let each copy of a widget differ. A new widget has no instance options, so it can look blank until the administrator sets them, which is why its default state should tell the administrator what to configure. Open the widget, choose the menu icon, select Edit option schema, then add an option with a label, name, type, hint and form section. Without a section, the option appears under Other options. Supported types are string, boolean, integer, reference, choice, field list, field name and glide list. Inside the widget, read values as options.optionName on the server or c.options.optionName on the client. Set defaults in the server script, for example options.text_color = options.text_color || “blue”;.
When you need a field type the schema lacks, or complex and searchable options, create a table that extends sp_instance, or sp_instance_table if you also need table and filter fields. Point the widget’s Data table field at it and choose the fields in the Fields slushbucket. A table is harder to maintain and adds server calls, so use it where the options need it.
Share logic and state with Angular Providers
Angular Providers are reusable components injected into several widgets. They keep client controllers light, because persistent data and logic live in one place for the lifetime of the portal. Create one under Portal Tables in Service Portal Configuration, choose a type (Directive, Factory or Service), and give it a name. Services are the better choice if you plan to move to ECMAScript 6 or later. Link the provider to a widget through the Angular Providers related list, then inject it by naming it as an argument in the client controller. Remove providers a widget no longer uses, since they complicate troubleshooting.
Attach libraries and icons with dependencies
A dependency package bundles JavaScript and CSS files. Create it under Service Portal, then Dependencies, and set a name, an application and whether it loads on the initial page. JS includes point to a URL or a UI script. CSS includes point to a style sheet in sp_css. Attach the package to the widget through its Dependencies related list. Dependencies load asynchronously, but each one adds download weight, so keep them small.
Font icons follow the same route. To scope an icon to one widget, attach the file to that widget and define the font face in its CSS field. To share it, place the CSS in a style sheet and include it as a dependency. That reaches each page the widget loads on, but unscoped CSS can disturb other styles.
Embed one widget inside another
Embedding reuses a widget without copying code. In a template, use the widget element with an ID and an options attribute holding JSON. In a client script, inject spUtil, call spUtil.get, and render the result with the sp-widget element. In a server script, call $sp.getWidget with the ID and an options object. A clock widget, for example, can be embedded with its own time zone and title set in the options attribute.
Embedding carries a widget’s full set of scripts, so for a small shared piece, an Angular directive is lighter. Directives suit shared scope, reusable subsections, common interface elements such as lists or avatars, and behavior extensions.
Write guidelines that keep widgets fast
Performance problems usually come from the volume of data and the frequency of server calls. Several habits prevent them:
- Limit queries with setLimit, tied to an option, for example gr.setLimit(options.limit || 100).
- Avoid auto-refreshing widgets, because each server.update(), spUtil.update(), server.refresh() or spUtil.refresh() runs the server script again.
- Filter recordWatch so it watches the fields you need.
- Use a publish and subscribe service instead of $broadcast, which reaches each child scope.
- Set initial state in the server script and use scripted REST APIs for later updates.
Localize widget text
Wrap user-facing text in the ${} syntax or gs.getMessage(). Both look up the sys_ui_message table, and ${message} is shorthand for ${gs.getMessage(“message”)}. Quotation marks in a client script translation can cause JavaScript errors, so fetch the string in the server script and bind it to a client variable.
Troubleshoot unexpected behavior
Start by isolating the fault. Set active to false on other widget records to stop their scripts running. Then open the browser developer console, and check syslog for warnings about large JSON objects, which point to a widget pulling too much data.
| Method | Where it runs | Output |
|---|---|---|
| console.log() | Server and client | Browser console |
| $sp.log() | Server | sp_log table, for sp_admin or admin users |
| gs.log(), gs.warn(), gs.error() | Server | syslog, which heavy use can slow |
| gs.addInfoMessage(), gs.addErrorMessage() | Server | Banner at the top of the window |
| spUtil.addInfoMessage() and related methods | Client | Message in the browser window |
Display problems are often access problems, not script faults. Run the ACL debugger to confirm the widget’s users can reach the records it reads. On a Service Catalog widget, the message “You are either not authorized or record is not valid” can mean the catalog item is hidden from the portal, so check that first. To probe a live widget, inspect its element and run angular.element($0).scope() in the console, then call $apply() after changing data.
Diagnose a portal page with widget diagnostics
Each widget has one of four customization levels. A Base widget is installed by the system or a plugin and is unmodified. A Cloned widget was copied from a base widget. A New widget was created from scratch. A Customized widget is a base widget changed in its code, option schema or related dependencies, templates or providers.
Choose Show Widget Customizations from the widget context menu. Green marks base, yellow cloned, blue new and red customized widgets. The information icon opens diagnostics where you can compare versions or compare a clone against its base. Investigate customized widgets first, because they are not supported in system upgrades. Revert them, then check new and cloned widgets. A clone made before an upgrade may lack the base widget’s updated code, so clone it again and reapply your changes. The tool can view and compare code but cannot edit it. It shows first-level dependencies, not deeper ones, and it cannot track a dependency after deletion.
Which accessibility defects do custom Service Portal widgets can introduce, and what should a developer build to prevent them?
Custom and cloned widgets can ship with click-only controls, incorrect HTML and ARIA, lost focus after data updates, inaccessible modal dialogs and missing accessible names. As of November 2026, no reliable public dataset ranks these by frequency, so treat them as risk-based priorities. Build each widget on native elements, handle keyboard and focus explicitly, and regression-test every clone.
Where the defects come from
A div with a click handler looks like a button on screen, but it takes no keyboard focus unless you add that yourself, and it does not announce itself as a control to a screen reader. Cloning adds a second risk: the copy has to be tested on its own, because a check of the base widget does not cover it.
Failure patterns to check in every custom widget
- Click-only controls. Actions built on generic elements cannot be reached or activated from the keyboard.
- Incorrect HTML and ARIA. A role that contradicts the element, or an expanded state that never changes.
- Lost focus after updates. A server round trip re-renders a panel, the focused element is removed, and focus drops to the top of the page.
- Inaccessible modal dialogs. Focus stays behind the dialog, Tab reaches the page underneath, and focus is not returned on close.
- Missing accessible names. Icon-only buttons can announce no name, so a screen reader user hears only that a button exists.
What a developer should build into each widget
- Reuse a maintained component before writing a custom one, and record which component the widget depends on.
- Use native elements first: a button for an action, a link for navigation, a label bound to each input.
- Give every control a text name in the markup, matching the visible wording where an icon is the label.
- Write keyboard handling for each custom interaction, and test that every action works with the keyboard alone.
- After server.update() or server.refresh(), move focus to the region the user was working in or to the result message.
Test the journey, not the widget in isolation
No single tool can decide whether a widget meets the standard. Combine automated checking with expert manual evaluation, assistive technology testing and testing with disabled users. Use WCAG 2.2 AA as the engineering target and check 2.1.1 Keyboard, 2.4.7 Focus Visible, 2.4.11 Focus Not Obscured and 4.1.2 Name, Role, Value. Test signed-in journeys end to end rather than widgets in isolation, and add a regression test for each change.
What to ask for before release
- A list of every custom and cloned widget, with its base widget and the date it was copied.
- Criterion-level evidence, the tested versions, defects found and their closure results, plus keyboard test notes for each changed journey.
- A written list of barriers still open. Do not claim full conformance while relevant failures remain.
Frequently asked questions
Do I need AngularJS experience to build a widget?
Yes for the HTML template and client script. The server script needs the ServiceNow API for record work.
Should I clone a base widget or start from scratch?
Clone when a base widget covers much of the need. Start from scratch when the behavior is new, since a clone stops receiving updates from its source.
Why does my widget look blank after I add it?
It has no instance options yet. Build a default state that tells the administrator what to set.
How do I see which widgets on a page were changed?
Use Show Widget Customizations in the context menu on the portal page.