Virtual Agent Topic Block: Retrieve Choice List - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Hi there,

With the Orlando release, ServiceNow introduced reusable Topic Blocks for Virtual Agent. Reusable components, which you could compare with Subflows or Actions. A really nice feature that helps build Topics smarter, faster, more consistent and manageable. Also less scripting knowledge is needed when working in Topics, because this logic has been set up in the Topic Block.

In a few short articles, I'll share some of the reusable Topic Blocks I've set up. These Topic Blocks can also be downloaded from Share.

Topic Blocks

A short recap:

Pre-Orlando
Basically pre-Orlando if we would have a repetitive group of steps for multiple Topics or even within a Topic, we simply had to duplicate them/build them over and over. There was no possibility to set up something like a Subflow. Well, maybe a small workaround would be doable for some situations, using vaSystem.switchTopic() or vaSystem.topicDiscovery().

Orlando
With the Orlando reusable a repetitive group of steps for multiple Topics or even within a Topic, can be placed within a "topic blocks". A short explanation of Topic Blocks according to the Docs site:

"A Topic block is basically a Subflow that performs certain actions or conversational tasks in a Topic."

Retrieve Choice List

When creating/updating Task related records, you will come across fields of type Choice List, and maybe even depending on Choice Lists. Can we avoid, hardcoding those choices, as they are already available in the Platform UI?

Out-of-the-box, ServiceNow has a GlideChoiceListGenerator API which returns you all choices for a certain field. With adding an additional parameter, the same API can also be used for returning all choices based on a dependent field. For example:

var choices = new GlideChoiceListGenerator('incident', 'category');
var choiceList = choices.get();

gs.info(choiceList);

Would result in:

*** Script: [-- None -- : , Inquiry / Help : inquiry, Software : software, Hardware : hardware, Network : network, Database : database]

When a dependent field is involved, the first line would for example look like:

var choices = new GlideChoiceListGenerator('incident', 'subcategory', 'software');

Knowing this, we could transform the code into usage for a User Input Reference Choice. To generate a list of choices dynamically. Translated into Virtual Agent this would look like:

(function execute() {
    
    var choices = new GlideChoiceListGenerator(vaInputs.table_name, vaInputs.choice_field);
    var choiceList = choices.get();

    var optionsArr = [];
    var l = choiceList.getSize();
    for(var i = 0; i < l; i++) {
        optionsArr.push({ 'value' : choiceList.getChoice(i).getValue(), 'label' : choiceList.getChoice(i).getLabel() });
    }

    return optionsArr;

})()

I did use vaInputs.table_name and vaInputs.choice_field here, these should be added as Input Parameters for the Topic Block.

With this, already have a basic working dynamic Retrieve Choice List Topic Block. You might want to consider filtering out the "-- None --" option.

Let's also add having a dynamic prompt message on the User Input Choice List, which can be added as Input Parameter for the Topic Block (vaInputs.choice_question). And what about having a dependent choice list?

The Virtual Agent script for a dependent choice list would be almost a copy of the first script. Only difference is var choices, like mentioned above:

(function execute() {
    
    var choices = new GlideChoiceListGenerator(vaInputs.table_name, vaInputs.dependent_field, vaInputs.retrieve_choice_list);
    var choiceList = choices.get();

    var optionsArr = [];
    var l = choiceList.getSize();
    for(var i = 0; i < l; i++) {
        optionsArr.push({ 'value' : choiceList.getChoice(i).getValue(), 'label' : choiceList.getChoice(i).getLabel() });
    }

    return optionsArr;

})()

Only two additional Input Parameters have to be added for the dependent choice, vaInputs.dependent_field and vaInputs.dependent_question. vaInputs.retrieve_choice_list is just the outcome of the first User Input Reference Choice.

To support having optionally the choice list or the choice list and the dependent choice list, we can simply leave the two Input Parameters for the dependent choice list not required. After the first User Input Reference Choice, only a Decision has to be added, to validate if the second User Input Reference Choice should be executed. Technically you could also do this using "Allow user to skip this node if:" Node Condition, though I prefer using a Decision because a decision helps to visualize the Topic flow.

find_real_file.png


Result

The above steps would result in a short Topic Block like:

find_real_file.png

Now we can simply use this Topic Block in Topics using the Topic Block utility.

The Topic Block Properties would ask you for some User Inputs. This would simply be the table name, a message which will be prompted, which choice field on the table mentioned it concerns. Optionally you could add the message which will be prompted for a dependent field and which dependent choice field on the table mentioned it concerns.

find_real_file.png

The ultimate result would be a choice list presented in the Virtual Agent chat, which is dynamically retrieved from the Platform UI.

find_real_file.png

Share

An Update Set with this Topic Block can be downloaded from Share:
- Virtual Agent Topic Block: Retrieve Choice List

---

And that's it actually. Hope you like it. If any questions, remarks or other ideas for Topic Blocks, let me know!

👍
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.

Interested in more articles, blogs, videos, and Share projects on Virtual Agent I published?
Virtual Agent


Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn

Comments
Aakash5
Tera Contributor

@Mark Roethof Can we use this topic block in the OOB report IT issue topic, so that user get to enter category and subcategory also and submit the incident.

yasseralkindi
Giga Explorer

@Aakash5 I have a very similar requirement, but not been able to make it work so far. 

Aakash5
Tera Contributor

Yes we can use the same topic block in the oob or any custom topic and it works. I tried and it worked for me. 

Praveen Kumar30
Tera Contributor

Hi @Mark Roethof 

Can we use this topic block in OOB Create HR case? If yes can you please help us how we can update the choice field of the HR case based on the user input. 

 

Thanks,

Praveen

Version history
Last update:
‎01-17-2021 10:06 PM
Updated by: