logo

NJP

Calling Events, REST, Flow Designer, Workflows through Virtual Agent

Import · May 03, 2020 · article

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:

image

image

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:

image

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:

image

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 image

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():

image

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:

image

---

And that's it. Hope you like it. If any questions or remarks, let me know!

image 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

image

View original source

https://www.servicenow.com/community/virtual-agent-nlu-articles/calling-events-rest-flow-designer-workflows-through-virtual/ta-p/2307335