Developing custom widgets
Summarize
Summary of Developing custom widgets
Develop custom widgets for ServiceNow portals using AngularJS, Bootstrap, and the ServiceNow API. Widgets enable dynamic, interactive components within Service Portal pages by combining server-side and client-side scripting to display data, gather user input, and manage state. Developing widgets requires knowledge of ServiceNow API to handle records and AngularJS to bind data and control client behavior. Bootstrap can optionally be leveraged for UI components.
Show less
Key Components of a Widget
- HTML Template (mandatory): Defines the user interface using AngularJS to render views and collect input.
- Client Script (mandatory): Uses AngularJS and ServiceNow API to map server data, process it, and facilitate interaction with the HTML template and server.
- Server Script (mandatory): Handles initial widget state, server queries, and data delivery to the client script.
- Link Function (optional): AngularJS function to manipulate the DOM directly.
- Option Schema (optional): Allows portal admins to configure widget parameters for flexible, reusable widgets.
- Angular Providers (optional): Reusable AngularJS components to share context, maintain state, and inject behavior across widgets.
- Dependencies (optional): External JavaScript or CSS files linked to widgets to include third-party libraries or styles.
Global Objects and Data Flow
When a widget is rendered, the server script first accesses global objects: input (data from client), options (widget instance parameters), and data (used to send data to client). The client script accesses server data via c.data and can update the server with server.update(). The c.options object contains read-only invocation parameters.
Development Environment and Best Practices
- Use the Widget Editor in Service Portal Configuration as a scripting IDE to edit widget parts and preview changes live.
- ES2021 (ES12) JavaScript mode is supported for server scripts depending on application settings.
- Follow general guidelines for performance, scalability, and user experience while developing widgets.
Practical Development Approaches
- Tutorial: Follow a guided example to build a custom widget displaying Service Catalog items.
- Clone a Widget: Start from existing widgets to expedite development.
- Create a Widget: Build from scratch for fully customized functionality.
- Widget Option Schema: Define configurable parameters for widget instances.
- Widget API Reference: Utilize client and server side APIs for widget scripting.
- Angular Providers: Develop reusable, persistent components to improve performance and maintainability.
- Embedded Widgets: Embed widgets within templates or scripts for modular design.
- Widget Dependencies: Link external scripts and stylesheets to widgets as needed.
- Internationalization: Use ${} or gs.getMessage() syntax to localize widget text content.
- Replacing UI Macros: Use widgets to replace UI Macros in Service Catalog forms for reusable, maintainable code.
Troubleshooting
Leverage available tools within Service Portal to diagnose and resolve unexpected widget behavior.
Develop custom widgets for portals using AngularJS, Bootstrap, and the ServiceNow API.
Before you begin
- Run record queries on the server.
- Create and update records.
- Bind variables to client controllers.
- Access server objects in a widget.
- Gather user input.
Optionally, you can build on the Bootstrap template by accessing Bootstrap components.
Parts of a widget
Like Angular directives, widgets execute a specified behavior within a Service Portal page. A widget includes mandatory and optional scripting components.
- HTML template
A mandatory widget component.
The HTML template requires knowledge of AngularJS to display and gather data. Use the HTML template to:
- Render the dynamic view that a user sees in the browser using information from the model and controller.
- Bind client script variables to your markup.
- Gather data from the end user.
- Client script
A mandatory widget component.
A client script requires knowledge of both the ServiceNow API and AngularJS to create a client controller. Use the client script to:
- Map server data from JavaScript and JSON objects to client objects.
- Process data before rendering it.
- Pass data to the HTML template.
- Pass user input and data back to the server for processing.
- Server script
A mandatory widget component.
A server script requires knowledge of the ServiceNow API to work with record data. Use the server script to:
- Set the initial state of the widget.
- Send record data to the widget client script using the data variable.
- Run server-side queries.
- Link function
An optional widget component.
The link function requires knowledge of AngularJS. Use a link function to directly manipulate the DOM.
- Option schema
An optional widget component.
Enables a Service Portal admin to configure a widget. Use the option schema to:
- Specify the parameters for a widget.
- Enable admin users to define instance options for a widget instance.
- Develop flexible, reusable widgets.
- Angular Providers
An optional widget component.
Angular Providers require knowledge of AngularJS. Use Angular Providers to:
- Keep widgets in sync when changing records or filters.
- Share context between widgets.
- Maintain and persist state.
- Create reusable behaviors and UI components and inject them into multiple widgets.
- Dependencies
An optional widget component.
A widget dependency is an external resource used by your widget such as JavaScript or CSS files.
Global objects in widgets
When a widget begins to render for the first time on a page, the server script executes first and accesses three global objects: input, options, and data. Because the input variable is a data object sent from the client script, this variable is undefined when first initialized.
When a widget is first instantiated, the server script:
- Initializes an empty data object.
- Initializes the input object with any data sent from the client controller, or the options object with any data used to initialize the widget.
- Sends the data object to the client controller as JSON.
The client script:
Accesses the server data object using
c.data.Note:By default, widgets use the c variable to represent the controller instance usingcontroller assyntax. You can change this variable when creating or copying widgets.Uses
server.update()to post changes to the data model. This method updates the server script using the input object.Note:After callingserver.update(), the client script data object is automatically overwritten by the server script data object.- Uses
c.optionsto access the values used to invoke the widget on the server. This object is read-only.
Using the Widget Editor
When you create a widget, a record is created in the sp_widget table. However, you can use the Widget Editor in Service Portal Configuration as your scripting environment. The Widget Editor is a full page application similar to an IDE. You can show the parts of the widget you want to edit and hide the rest, while previewing your changes in real time.