spUtil - Client

  • 릴리스 버전: Australia
  • 업데이트 날짜 2026년 03월 12일
  • 소요 시간: 17분
  • 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.

    spUtil - addErrorMessage(String message)

    Displays a notification error message.

    표 1. Parameters
    Name Type Description
    message String Error message to display.
    표 2. Returns
    Type Description
    void
    spUtil.addErrorMessage("There has been an error processing your request")

    spUtil - addInfoMessage(String message)

    Displays a notification info message.

    표 3. Parameters
    Name Type Description
    message String Message to display.
    표 4. Returns
    Type Description
    void
    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.

    표 5. Parameters
    Name Type Description
    message String Message to display.
    표 6. Returns
    Type Description
    void
    spUtil.addTrivialMessage("Thanks for your order")

    spUtil - createUid()

    Create a unique identifier.

    표 7. Parameters
    Name Type Description
    None
    표 8. Returns
    Type Description
    String A unique 32 character ID.

    spUtil - get(String widgetId Object data)

    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.

    표 9. Parameters
    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.
    표 10. Returns
    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;
      });  	
    

    spUtil - getHeaders()

    Retrieves all headers to use for API calls.

    표 11. Parameters
    Name Type Description
    None
    표 12. Returns
    Type Description
    Object All headers to use for API calls.

    spUtil - getHost()

    Returns the complete host domain.

    표 13. Parameters
    Name Type Description
    None
    표 14. Returns
    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.

    표 15. Parameters
    Name Type Description
    preference String Name of the preference.
    callback Function Define the callback function.
    표 16. Returns
    Type Description
    void

    spUtil - getURL()

    Returns the current service portal URL information.

    표 17. Parameters
    Name Type Description
    None
    표 18. Returns
    Type Description
    String Current service portal URL.

    spUtil - format(String template, Object data)

    Formats a string that contains variables.

    Use this method as an alternative to string concatenation.

    표 19. Parameters
    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.
    표 20. Returns
    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.

    표 21. Parameters
    Name Type Description
    None
    표 22. Returns
    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.

    표 23. Parameters
    Name Type Description
    attributes String String containing comma separated attributes, such as the Attributes field of a dictionary record.
    표 24. Returns
    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.

    주:
    When passing the $scope argument into the recordWatch() function, inject $scope into the parameters of your client script function.
    표 25. Parameters
    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.
    표 26. Returns
    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.

    표 27. Parameters
    Name Type Description
    $scope Object Scope defined for the update.
    표 28. Returns
    Type Description
    Object Updated options and data objects.

    spUtil - scrollTo(String selector, Number time)

    Scrolls to the element with the specified selector, over a specified period of time.

    표 29. Parameters
    Name Type Description
    selector String Selector to scroll to.
    time Number Time taken to scroll to the specified selector.

    Unit: Milliseconds

    표 30. Returns
    Type Description
    void

    spUtil - setBreadCrumb(Object $scope, Array breadcrumbs)

    Updates the header breadcrumbs.

    표 31. Parameters
    Name Type Description
    $scope Object Scope defined for the table.
    breadcrumbs Array Conditions used to create the breadcrumb filter.
    표 32. Returns
    Type Description
    void

    spUtil - setPreference(String pref, String value)

    Sets a user preference.

    표 33. Parameters
    Name Type Description
    pref String Preference name
    value String Preference value
    표 34. Returns
    Type Description
    void

    spUtil - setSearchPage(String searchPage)

    Updates the search page.

    표 35. Parameters
    Name Type Description
    searchPage String Name of the search page.
    표 36. Returns
    Type Description
    void

    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.

    표 37. Parameters
    Name Type Description
    $scope Object Scope defined for the update.
    표 38. Returns
    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);
       }
    });