spContextManager - Client

  • Release version: Australia
  • Updated March 12, 2026
  • 3 minutes to read
  • Makes data from a Service Portal widget available to other applications and services in a Service Portal page. For example, pass widget data to Agent Chat when it opens in a Service Portal page.

    The spContextManager API is an AngularJS service that you can use in Service Portal widget client scripts.

    Keys passed to this API are unique per page. For example, if the 'agent-chat' key is already initialized by another widget on the page through the addContext() method, you must use the updateContextForKey() method to update the key's data. Available keys include: agent-chat: Sends widget data to Agent Chat.

    For more information about passing data to Agent Chat, see Configure Agent Chat in Service Portal.

    spContextManager - addContext(String key, Object context)

    Initializes a key and adds widget data as the value. For example, add data to the 'agent-chat' key to make it available to Agent Chat.

    Use this method the first time data is added to a specific key on a Service Portal page. If the key is already used by another widget on the page, use the updateContextForKey() method instead.

    Table 1. Parameters
    Name Type Description
    key String Name of the key to send the data.

    Available keys includeagent-chat: Sends widget data to Agent Chat when it opens in a Service Portal page.

    context Object Widget data in JSON format to send to the application or service specified in the key parameter. For example, {'approval_count': 5}.
    Table 2. Returns
    Type Description
    void

    Pass approval_count to Agent Chat. When a user initiates an Agent Chat conversation from the Service Portal homepage, the system appends &sysparm_approval_count=5 to the Agent Chat iframe URL.

    function ($scope, spContextManager) {
        spContextManager.addContext('agent-chat', {
            'approval_count': 5       
        });
    };

    spContextManager - getContext()

    Returns each key and associated data object defined by any widget on the page.

    Using this method may affect performance. Use this method to understand which keys are initialized on the page and to get their current values. If you know which key you need to access, use the getContextForKey() method instead.

    Table 3. Parameters
    Name Type Description
    None
    Table 4. Returns
    Type Description
    Object Each key and associated data object defined on the page.
    function ($scope, spContextManager) {
      spContextManager.getContext();
    } 

    spContextManager - getContextForKey(String key, Boolean returnPromise)

    Returns the widget data associated with a key.

    Table 5. Parameters
    Name Type Description
    key String Name of the key to get context from. Available keys include: agent-chat: Sends widget data to Agent Chat.
    returnPromise Boolean Flag that determines whether to return the data associated with a key as a promise or an object. Values include:
    • True: return the data as a promise. Use this option if another widget on the page uses the addContext() method to initialize the same key. Returning a promise prevents returning an undefined object when the key is not yet initialized.
    • False: returns an object containing the data associated with the key.
    Table 6. Returns
    Type Description
    Promise If returnPromise is true, returns a promise that is fulfilled when another widget on the page initializes the key.
    Object If returnPromise is false, returns an object containing the data associated with the key. For example, {approval_count: 5}.

    Pass approval_count to Agent Chat. When a user initiates an Agent Chat conversation from the Service Portal homepage, the system appends &sysparm_approval_count=5 to the Agent Chat iframe URL.

    function ($scope, spContextManager) {
      spContextManager.getContextForKey('agent-chat', true).then(function(context) {
        context = context || {};
        context.approval_count = 5; 
        spContextManager.updateContextForKey('agent-chat', context);
      });
    } 

    spContextManager - updateContextForKey(String key, Object context)

    Sends data to an existing key. For example, if another widget on the page uses the 'agent-chat' key to pass data to the Agent Chat configuration, you must update the context of the key rather than using the addContext() method.

    Table 7. Parameters
    Name Type Description
    key String Name of the key to send the data.

    Available keys includeagent-chat: Sends widget data to Agent Chat when it opens in a Service Portal page.

    context Object Widget data in JSON format to send to the application or service specified in the key parameter. For example, {'approval_count': 5}.
    Table 8. Returns
    Type Description
    void

    Pass approval_count to Agent Chat. When a user initiates an Agent Chat conversation from the Service Portal homepage, the system appends &sysparm_approval_count=5 to the Agent Chat iframe URL.

    function ($scope, spContextManager) {
      spContextManager.getContextForKey('agent-chat', true).then(function(context) {
        context = context || {};
        context.approval_count = 5; 
        spContextManager.updateContextForKey('agent-chat', context);
      });
    }