Calling Events, REST, Flow Designer, Workflows thr... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Hi there,

After working with Virtual Agent for a while, you might cross a point thinking "Can we technically smarten our Virtual Agent topics? Can we initiate or perform more complex operations?" For example like performing a REST call, or starting a Workflow? For example because of initiating password resets, retrieving information from other systems, etcetera.

Of course, we can!

Setting-up a test Topic

Let's draw up a test Topic within the Virtual Agent Designer:

find_real_file.png

find_real_file.png


Event

For this example, we've set up a plain event registry. This event registry could for example be used to trigger a notification or a scheduled job. Within the Script Action Utility in the Virtual Agent Designer, you can create an Event with the code below. The name correspondents exactly with the name of your event registry.

Script

(function execute() {

    gs.eventQueue('Caling Workflows, REST, etc');

})()

Note: You could pass more parameters to eventQueue, though in this example we are only after creating the event.

Result

After selecting Event in the Virtual Agent topic, the Event log in the Platform UI would show an event has been created:

find_real_file.png


Flow Designer

For this example, we've set up a basic flow that only creates a log message. Within the Script Action Utility in the Virtual Agent Designer, you can start a Flow Designer with the code below. The name needed corresponds with the internal name of the Flow.

Script

(function execute() {

    sn_fd.FlowAPI.executeFlow('calling_workflows_rest_etc');

})()

Note: You could pass more parameters to executeFlow, though in this example we are only after starting the flow.

Result

After selecting Flow Designer in the Virtual Agent topic, the Today's Executions (Flow engine contexts) in the Platform UI would show a Flow has been started:

find_real_file.png


REST

For this example, we created the basic code needed for a Retrieving a record (GET) through the REST API Explorer in the Platform UI. The record to retrieve in this example is out-of-the-box Incident INC0000002. Within the Script Action Utility in the Virtual Agent Designer, you can start a Workflow with the code below. Obviously, update the user/password 🙂

Script

(function execute() {

    var request = new sn_ws.RESTMessageV2();
    request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/table/incident/9d385017c611228701d22104cc95c371');
    request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
    var user = 'admin';
    var password = '********';

    request.setBasicAuth(user, password);
    request.setRequestHeader("Accept", "application/json");

    var response = request.execute();

    vaVars.response = response.getBody();

})()

Result

After selecting REST in the Virtual Agent topic, the Tekst Bot Response would display the response.getBody():

find_real_file.png


Workflow

For this example, we've set up a basic workflow that only creates a log message. Within the Script Action Utility in the Virtual Agent Designer, you can start a Workflow with the code below. Copy the sys_id needed for example through searching your workflow in the wf_workflow table, and performing "Copy sys_id".

Script

(function execute() {

    var w = new Workflow();
    var context = w.startFlow('48b3ee292f67c850b0c2d5ea2799b658');

})()

 Note: You could pass more parameters to startFlow, though in this example we are only after starting the workflow.

Result

After selecting Workflow in the Virtual Agent topic, the Workflow Contexts in the Platform UI would show a workflow has been started:

find_real_file.png

---

And that's it. Hope you like it. If any questions or remarks, 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
Vivek Verma
Mega Sage
Mega Sage

Excellent Monday morning dose.

Thank you.

Bikramjit1
Kilo Contributor

Excellent information

Eli Guttman
Tera Guru

Hi, thank you for this article!

Do you know if there is an option to "wait" for response? i have a process that active SSH call using workflow and i need to wait for the response.

Is there something like "timer" that can hold the process few about 10 seconds  that can be used in the virtual agent?

Mark Roethof
Tera Patron
Tera Patron

Hi there,

See my articles list. 2 weeks ago I published an article on a Topic Block "Wait N Seconds". There's also a Share download.
Community article list

It might not be best practice / recommended by ServiceNow, though there's no suitable other option (and something similar is actually being used in one of the out-of-the-box template topics).

Eli Guttman
Tera Guru

thank you very much! i'll give it a try.

I'm a bit concerned regarding the last comment on that page - is there a risk to use this method? have you run into an issue with it? 

Mark Roethof
Tera Patron
Tera Patron

For myself: Found no issues at all.

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

---

LinkedIn
Community article list

abirakundu23
Mega Sage

Hello @Mark Roethof ,

I have tried to implement of my PDI, getting errors for event and flow.

Flow internal name and event name is present in my instances.

Added script in Action expression and script part both. Please let me know how can fixed.

find_real_file.png

find_real_file.png

 

 

Mark Roethof
Tera Patron
Tera Patron

What is the logging showing you? Most of the times, the logging of Virtual Agent is really helpful.

Anyway, just looking at what you shared:

What is "etc" in your code? Same for "REST" in your code? That will probably be your issue.

Kind regards,
Mark

Version history
Last update:
‎05-03-2020 01:00 AM
Updated by: