Client API reference › API reference › API implementation and reference
spUtil- Client
The spUtil API provides utility methods to perform common functions in a Service Portal widget client script.
These functions include:
For additional information on widgets, see Service Portal widgets.
Parent Topic:Client API reference
spUtil - addErrorMessage(String message)
Displays a notification error message.
| Name | Type | Description |
|---|
| message | String | Error message to display. |
spUtil.addErrorMessage("There has been an error processing your request")
spUtil - addInfoMessage(String message)
Displays a notification info message.
| Name | Type | Description |
|---|
| message | String | Message to display. |
spUtil.addInfoMessage("Your order has been placed")
spUtil - addTrivialMessage(String message)
Displays a trivial notification message.
Trivial messages disappear after a short period of time.
| Name | Type | Description |
|---|
| message | String | Message to display. |
spUtil.addTrivialMessage("Thanks for your order")
spUtil - createUid()
Create a unique identifier.
| Type | Description |
|---|
| String | A unique 32 character ID. |
Embeds a widget model in a widget client script.
The callback function returns the full widget model. For additional information on widgets, see Service Portal widgets.
| Name | Type | Description |
|---|
| widgetId | String | Widget ID or sys_id of the widget to embed. |
| data | Object | Optional. Name/value pairs of parameters to pass to the widget model. |
| Type | Description |
|---|
| Object | Model of the embedded widget. |
Without data passed.
spUtil.get("widget-cool-clock").then(function(response) {
c.coolClock = response;
});
With data passed.
spUtil.get('pps-list-modal', {title: c.data.editAllocations,
table: 'resource_allocation',
queryString: 'GROUPBYuser^resource_plan=' + c.data.sysId,
view: 'resource_portal_allocations' }).then(function(response) {
var formModal = response;
c.allocationListModal = response;
});
Retrieves all headers to use for API calls.
| Type | Description |
|---|
| Object | All headers to use for API calls. |
spUtil - getHost()
Returns the complete host domain.
| Type | Description |
|---|
| String | The complete host domain, for example hi.servicenow.com |
spUtil - getPreference(String preference, Function callback)
Executes the callback with User Preference response by passing the preference name.
| Name | Type | Description |
|---|
| preference | String | Name of the preference. |
| callback | Function | Define the callback function. |
spUtil - getURL()
Returns the current service portal URL information.
| Type | Description |
|---|
| String | Current service portal URL. |
Formats a string that contains variables.
Use this method as an alternative to string concatenation.
| Name | Type | Description |
|---|
| template | String | String template that contains values for variable substitution. |
| data | Object | Object containing the values for the variables defined in the template string. |
| Type | Description |
|---|
| String | String containing the variable values instead of the variable nomenclature. |
spUtil.format('An error ocurred: {error} when loading {widget}', {error: '404', widget: 'sp-widget'})
Output:
'An error occurred: 404 when loading sp-widget'
spUtil - isMobile()
Checks if the current client is a mobile device.
| Type | Description |
|---|
| Boolean | Flag that indicates whether the current client is a mobile device.Valid values: - true: Current client is a mobile device. - false: Current client is not a mobile device. |
spUtil - parseAttributes(String attributes)
Parses the comma-separated attributes within a specified string.
| Name | Type | Description |
|---|
| attributes | String | String containing comma separated attributes, such as the Attributes field of a dictionary record. |
| Type | Description |
|---|
| Array | Array of objects containing the parsed attributes. |
function getRefQualElements() {
var refQualElements = [];
if (field && field.attributes && field.attributes.indexOf('ref_qual_elements') > -1) {
var attributes = spUtil.parseAttributes(field.attributes);
refQualElements = attributes['ref_qual_elements'].split(';');
}
return refQualElements;
}
spUtil - recordWatch(Object $scope, String table, String filter, Function callback)
Watches for updates to a table or filter and returns the value from the callback function.
Allows a widget developer to respond to table updates in real-time. For instance, by using recordWatch(), the Simple List widget can listen for changes to its data table. If records are added, removed, or updated, the widget updates automatically.
Note: When passing the $scope argument into the recordWatch() function, inject $scope into the parameters of your client script function.
| Name | Type | Description |
|---|
| $scope | Object | Scope of the data object updated by the callback function. |
| table | String | Watched table. |
| filter | String | Filter for fields to watch. |
| callback | Function | Optional. Parameter to define the callback function. |
| Type | Description |
|---|
| Promise | Return value of the callback function. |
//A simple recordWatch function.
spUtil.recordWatch($scope, "live_profile", "sys_id=" + liveProfileId);
//In a widget client script
function(spUtil, $scope) {
/* widget controller */
var c =this;
// Registers a listener on the incident table with the filter active=true,
// meaning that whenever something changes on that table with that filter,
// the callback function is executed.
// The callback function takes a single parameter 'response', which contains
// the property 'data'. The 'data' property contains information about the changed record.
spUtil.recordWatch($scope, "incident", "active=true", function(response) {
// Returns the data inserted or updated on the table
console.log(response.data);
});
}
spUtil - refresh(Object $scope)
Calls the server and replaces the current options and data with the server response.
Calling spUtil.refresh() is similar to calling server.refresh(). However, when you call spUtil.refresh(), you can define the $scope object.
| Name | Type | Description |
|---|
| $scope | Object | Scope defined for the update. |
| Type | Description |
|---|
| Object | Updated options and data objects. |
Scrolls to the element with the specified selector, over a specified period of time.
| Name | Type | Description |
|---|
| selector | String | Selector to scroll to. |
| time | Number | Time taken to scroll to the specified selector.Unit: Milliseconds |
spUtil - setBreadCrumb(Object $scope, Array breadcrumbs)
Updates the header breadcrumbs.
| Name | Type | Description |
|---|
| $scope | Object | Scope defined for the table. |
| breadcrumbs | Array | Conditions used to create the breadcrumb filter. |
spUtil - setPreference(String pref, String value)
Sets a user preference.
| Name | Type | Description |
|---|
| pref | String | Preference name |
| value | String | Preference value |
spUtil - setSearchPage(String searchPage)
Updates the search page.
| Name | Type | Description |
|---|
| searchPage | String | Name of the search page. |
spUtil - update(Object $scope)
Updates the data object on the server within a given scope.
This method is similar to server.update(), but includes a $scope parameter that defines the scope to pass over.
| Name | Type | Description |
|---|
| $scope | Object | Scope defined for the update. |
| Type | Description |
|---|
| Object | Updated data object. |
The following example includes a P1 widget that watches for changes to the state field and uses a filter to watch all active P1s and let the callback function determine whether to refresh the data. The data.changes property contains an array of any updated fields. If the state of any fields have changed, the data is updated in the widget.
var q = "priority=1^active=true^EQ";
spUtil.recordWatch($scope, "incident", q, function(event, data) {
if (data.changes.includes("state")) { // only update if state was updated.
spUtil.update($scope);
}
});