how to keep variables in same session?

asilism
Tera Contributor

Hello, 

 

I made 2 topic blocks, named "Module 1" and "Module 2"

 

[Module1]

start -> script -> Module2 -> end

 

[Module2]

start -> script -> end

 

if I define some variables (vaVars.test) in "Module 1" and move to Topic block "Module 2" using by Topic block then I confirm value, that variables are null in Module 2. 

of course, I also try define script variables Module1 and Module2 both side, same name.

 

how to keep variables in same message session? I want to know the lifecycle of a variable.

is there only way to insert/get glide record?

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage
Mega Sage

Hi @asilism ,

 

To keep variables in the same session between different topics, you can store them in the conversation context using the gs.getSession().putClientData(key, value) method in ServiceNow. This will store the variable value in the session and can be accessed across different topics.

Here is an example of how you can store the value of vaVars.test in the conversation context:

 

gs.getSession().putClientData('test_var', vaVars.test);

 

 

And here is an example of how you can retrieve the value of test_var from the conversation context:

 

var testVar = gs.getSession().getClientData('test_var');

 

You can also use gs.getSession().putClientData(key, value) and gs.getSession().getClientData(key) methods to store and retrieve script variables.

Using a GlideRecord to store the value is also an option, but it can be a more complex solution compared to storing the variable in the conversation context.

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

View solution in original post

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @asilism ,

 

To keep variables in the same session between different topics, you can store them in the conversation context using the gs.getSession().putClientData(key, value) method in ServiceNow. This will store the variable value in the session and can be accessed across different topics.

Here is an example of how you can store the value of vaVars.test in the conversation context:

 

gs.getSession().putClientData('test_var', vaVars.test);

 

 

And here is an example of how you can retrieve the value of test_var from the conversation context:

 

var testVar = gs.getSession().getClientData('test_var');

 

You can also use gs.getSession().putClientData(key, value) and gs.getSession().getClientData(key) methods to store and retrieve script variables.

Using a GlideRecord to store the value is also an option, but it can be a more complex solution compared to storing the variable in the conversation context.

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar