IT CanvassTalk to an advisor
Development · LessonBy , ServiceNow Trainer, 9 yrs · Published · ServiceNow · all levels

Service Portal widgets

How a widget is built: server script, client controller, HTML template, options and data flow.

Quick answer

How a widget is built: server script, client controller, HTML template, options and data flow.

Key takeaways
  • data carries server results to the client, input carries client requests back
  • Option schemas make widgets reusable across pages
  • Keep the server script lean, it runs on every load
  • Use existing widgets as a base before writing a new one

The three parts

A widget has a server script that runs once and fills the data object, an HTML template rendered by AngularJS, and a client controller that handles interaction. The data object is the bridge between server and client.

Server to client and back

The client calls the server again with c.server.update() or c.server.get(). Anything on input arrives in the server script as input, and anything on data returns to the client.

// server script
data.openCount = new GlideAggregate('incident');
// ...
if (input && input.action === 'refresh') {
  data.refreshedAt = new GlideDateTime().getDisplayValue();
}

// client controller
c.refresh = function() {
  c.data.action = 'refresh';
  c.server.update().then(function() { c.data.action = undefined; });
};

Options and reuse

Declare an option schema so the same widget can be dropped on several pages with different titles, tables or filters. Hard coding a table name inside a widget is the fastest way to end up with five near identical widgets.

Performance

The server script runs on every page load. Keep queries narrow, use GlideAggregate for counts, and avoid loading a hundred records to show five.

Practice challenge

+0 XPStreak ×0
Question 1 of 2
Which object returns server results to the template?

Frequently asked questions

Do I need Angular experience?
Basic Angular 1 concepts help: controllers, scope and directives. Most portal work is small controllers plus templates.
Widget or UI Builder?
Service Portal widgets remain right for portal pages today. UI Builder is the direction for workspace and newer experiences, so new greenfield interfaces are worth evaluating there.
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support