Developing custom widgets
Summarize
Summary of Developing custom widgets
Developing custom widgets in ServiceNow's Service Portal enables you to create dynamic, interactive portal components using AngularJS, Bootstrap, and the ServiceNow API. This capability lets you build tailored user experiences by integrating server-side data and client-side interactions seamlessly within your portal pages.
Show less
Key Features
- Widget Components:
- HTML Template: Mandatory for rendering the user interface and binding client variables using AngularJS.
- Client Script: Mandatory controller script that manages client-side logic, data binding, user input, and communication with server scripts.
- Server Script: Mandatory script that initializes widget state, runs server-side queries, and sends data to the client.
- Link Function (Optional): Manipulate DOM elements directly using AngularJS.
- Option Schema (Optional): Enables configuration parameters for widget instances, allowing admins to customize widget behavior.
- Angular Providers (Optional): Create reusable, shared components to maintain state and sync data across multiple widgets efficiently.
- Dependencies (Optional): Link external JavaScript or CSS files to extend widget functionality with third-party libraries or styles.
- Global Objects: Widgets use
input,options, anddataobjects to manage data flow between server and client scripts, withserver.update()used to synchronize changes. - Widget Editor: A dedicated IDE-like environment within Service Portal Configuration to create, edit, and preview widgets live.
- Internationalization: Support for localizing widget text using ServiceNow’s translation syntax.
Practical Guidance for ServiceNow Customers
- Prerequisites: Experience with ServiceNow API and AngularJS is required to create effective widgets, including server data handling and client-side binding.
- Development Tips: Follow best practices such as using Angular Providers to improve performance and scalability, and leverage the Widget Editor for efficient development.
- Reuse and Extend: Clone existing widgets to accelerate development or create new ones from scratch. Use embedded widgets and dependencies to modularize and enhance widget capabilities.
- Configuration: Use the option schema to make widgets configurable per instance, empowering admins to tailor widgets without code changes.
- Troubleshooting: Utilize provided tools within the platform to diagnose and resolve widget issues effectively.
Expected Outcomes
By developing custom widgets, you can deliver personalized, responsive, and maintainable portal experiences that integrate complex data and interactions. Widgets built with AngularJS and the ServiceNow API ensure a robust connection between client and server, enhancing user engagement and portal functionality.
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.
Allows a Service Portal admin to configure a widget. Use the option schema to:
- Specify the parameters for a widget.
- Allow 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 cloning 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.