Can we trigger a client script when a client state changes( updated using a inherited controller)

akshitbadya
ServiceNow Employee
 
1 REPLY 1

Matthew_13
Kilo Sage

Yes, but only under specific conditions that I have found.

What works true client-side state change

If the State field changes in the browser (user edits it, or your inherited controller sets it on the client model / g_form.setValue()), then you can trigger client logic via:

  • onChange Client Script on the state field (classic form / UI16)

  • In Service Portal / Angular: $scope.$watch() on the model field, or trigger logic immediately after you set it

  • In Workspace / UI Builder: client logic tied to the record/form state (depending on the component) and/or event handlers

What does NOT work server-side update

If the state is updated on the server Business Rule, Flow, Script Include, API, transform, etc no client script will “fire” automatically because client scripts only run when something changes in the user’s browser session.

In that case, you have two common patterns:

  1. Record watch / live update (best for portal/workspace)

    • Service Portal: spUtil.recordWatch() to detect server-side updates and then run client logic.

    • Workspace/UI Builder: use a record/data refresh mechanism (record watcher / refresh data broker / component event) to re-fetch the record and then react to the new state.

  2. Force a client refresh / re-query

    • After the server update completes, refresh the form/data and then run logic based on the refreshed state.

Practical rule of thumb

  • Changed in UI → onChange / client watcher works.

  • Changed on server → you need record watch or refresh to detect it on the client.

If you tell me whether you mean Service Portal Angular/inherited controller or Workspace/UI Builder I can trry and give you the exact method and a short example for that UI if needed.

 

@akshitbadya  - Please mark as Accepted Solution and Thumbs Up if you find Helpful!!