Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

How to store data for the current user session in UI Builder?

Tejas Adhalrao
Kilo Sage

Hi everyone,

I'm working with UI Builder (Now Experience) and have the following requirement:

  • When a user opens the page for the first time in a login session, the acknowledgement container should be displayed.

  • Once the user clicks Acknowledge, the acknowledgement should be hidden.

  • If the user revisits the same page during the same login session, the acknowledgement should remain hidden.

  • After the user logs out or the session expires and they log in again, the acknowledgement should be displayed again.

Constraints:

  • gs.getSession() is not available in my environment.

  • sessionStorage is not accessible from UI Builder client scripts.

Is there any supported ServiceNow approach to store data only for the duration of a user's login session or detect when a user's session ends in UI Builder?

Thanks!

1 REPLY 1

Ryan166
Tera Contributor

Hello Tejas, 

 

I can't understand why you cannot use gs.getSession. It is server side so you won't be able to use it from UX Client Script but you could create a data resource. The data resource would be linked to a Script Include using functions as follows : 

isAcknowledged: function() {
        return gs.getSession().getClientData('ack_shown') == 'true';
    },

    setAcknowledged: function() {
        gs.getSession().putClientData('ack_shown', 'true');
        return true;
    },

 Then use a state variable to store the value and use this variable to control button visibility. When button is clicked you should : 

  • Update variable state value 
  • launch function "setAcknowledged" from data resource

Please let me know it this helps 🙂