Synchronous api.setState() in UI Builder

IronPotato
Mega Sage

Hi All,

 

I'd like to ask, whether anyone have experience with setting client state in UI builder but synchronously using callback function? 

 

I'm trying to change state of client state but it always give me old value. I need that value to be up to date.

In ServiceNow documentation there's an example with the object but I need to implement that with simple boolean true false value.

 

Example from docs

IronPotato_1-1704443004423.png

 

UPDATED Funny behavior 

IronPotato_0-1704444258779.pngIronPotato_1-1704444273079.png

 

 

any help would be appreciated.

 

Thank you

 

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

You can use simple statement.

api.setState('variable_name', 'value');

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi @Anil Lande ,

 

that's async type of setState. I would always get old value.

 

IronPotato_0-1704444453245.pngIronPotato_1-1704444478387.png

 

hardikparpelli
ServiceNow Employee
ServiceNow Employee

@IronPotato

 

The arrow function will get called after the execution of this script. You can verify this by adding debugger; statement in the arrow function.

This callback is to update the state parameter immediately after the execution of this script so that the updated value is available in the next codeflow.

 

Within this function, you can work with the source value that you are using to update the state parameter.

 

Refactor tip: updating currentValue is not required. Whatever this arrow function returns will be the new value of the state parameter. So you can just do: return result;