Virtual Agent Topic Block: Record Count - 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". The 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."


Record Count

A functionality I use quite often, performing a record count. For example when one would query user records, to verify if there was any result or not, exactly one user was found, or multiple users were found. Knowing this, you could enhance your Topic with appropriate logic for follow-up or error handling.
So are you going to script this in all of your Topics, maybe even several times in a Topic, or are you going to make this more generic? Using a Script Include or… reusable Topic Blocks!

If we would script a Record Count, for example all active incidents, we could use something like:

var answer = 0;
        
var countIncidents = new GlideAggregate('incident');
countIncidents.addQuery('active', true);
countIncidents.addAggregate('COUNT');
countIncidents._query();
    
if(countIncidents._next()) {
    answer = countIncidents.getAggregate('COUNT');
}

Translating this into usage for Virtual Agent utility Script Action, and making this more dynamic we could come up with something like:

(function execute() {

    var answer = 0;
        
    var countRecords = new GlideAggregate(vaInputs.table_name);
    countRecords.addEncodedQuery(vaInputs.encoded_query);
    countRecords.addAggregate('COUNT');
    countRecords._query();
    
    if(countRecords._next()) {
        answer = countRecords.getAggregate('COUNT');
    }
        
    vaVars.record_count = answer;

})()

Added a vaInputs for the table and using Encoded Query, also with a vaInputs. vaInputs because we could define both as mandatory Input Parameters on the Topic Block.

find_real_file.png

The Script Action would result in a vaVars.record_count. We can add this as an Output Parameter on the Topic Block.

find_real_file.png

By default, the Script Variable record_count will not be available using the Data Pill. We would need to set up a session-based variable in the Script Variables section for this. Read more about this in one of my previous articles: Using Virtual Agent vaVars through the data pill.

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 to supply a table_name and encoded_query (both mandatory). Encoded queries can easily be set up using the breadcrumbs on Lists. Read more about this in one of my previous articles: Utilizing the breadcrumb on lists to generate your query.

find_real_file.png

The ultimate result would be that the record count is now available as vaVars.record_count! Available with just easy drag and drop reusable Topic Block.


Share

An Update Set with this Topic Block can be downloaded from Share:
- Virtual Agent Topic Block: Record Count

---

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

Version history
Last update:
‎10-26-2020 04:46 AM
Updated by: