How to pass Value from one topic to another in Virtual Agent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 02:05 AM
Hi All,
I have a requirement of passing a variable from one topic to another topic.
I am defining one variable like this to store the user input:
In another topic I am parsing one system Property like this:
But in logs I can see in the second topic where I am fetching the value, it showing selectedCategory is not defined.
How I can make it work so that selectedCategory should store correct value that is Hardware, to give it's corresponding sysId.
Kindly Guide.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:42 PM
@Mark Roethof
Sorry I have defined it like this in second topic
But I am getting the value of vaContext.LiveAgent_selectedCategory as undefined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 12:40 AM
@Mark Roethof Can you please guide.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2025 12:01 PM
Here is how I did it.
Topic 1:
We have a text input module variable called username.
The user enters their name and the value is saved as "username"
Then use a Script module to assign "username" to a Context variable like this:
(function execute() {
var name = vaInputs.username.getValue();
vaContext.LiveAgent_user_name = name;
// Get the actual user GlideRecord
var userId = gs.getUserID(); // Get sys_id of current user
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
userGR.u_chat_display_name = name;
userGR.update();
}
})();
Then I created a new field on the sys_user table called u_chat_display_name (This new field does not have to be in this location. A custom table would be better, that way you can add more fields to it to store more information.)
I created the context variable "user_name" as a context variable which now is LiveAgent_user_name.
(Context variables can be created here: conversational interfaces > settings > general > context variable> New)
(ServiceNow puts LiveAgent_ before the var automatically)
So in the script above we are:
1. Getting the value of username (the user input from the text input module)
2. Saving that input as "name"
3. Then we are saving "name" as the value of the context variable (LiveAgent_user_name)
4. Im getting the current logged in users sys_id
5. Doing a GlideRecord in to the sys_user table
6. Checking to ensure the userId (users sys_id) exists
if it exists im saving "name" as "u_chat_display_name"
7. Then updating the record.
The u_chat_display_name field will dynamically update with the value the user enters in the beginning for "username"
Next you can ensure your context variable is getting a value by debugging as follow
Drag a bot response text module below the script module and use this function:
(function execute() {
return "Name is: " + vaContext.LiveAgent_user_name;
})();
Now the bot response will return the value the user entered in the very beginning for "username"
(This step is NOT needed but will tell you the value on the context variable to ensure its being defined.)
Topic 2:
All I have in topic 2 is as follows:
A bot respost text module with the following code
(function execute() {
var userGR = new GlideRecord('sys_user');
if (userGR.get(gs.getUserID()) && userGR.u_chat_display_name) {
return "Welcome back, " + userGR.u_chat_display_name + "!";
} else {
return "Hi there! What should I call you?";
}
})();
Heres what we are doing:
1. Creating a GlideRecord on the sys_user table.
2. Getting the current user with gs.getUserID().
3. If the user exists and there is a value in u_chat_display_name
4. If it does → returns "Welcome back, [u_chat_display_name]!".
5. If not → returns "Hi there! What should I call you?".
Thanks
-Dustin