Can Virtual Agent generate an email?

Keszia
Giga Guru

Hi everyone - hoping there's some smarts out there that can offer ideas on this.

I'm wanting to design a topic that generates an email to a shared mailbox, is there a script that creates this experience?

1 ACCEPTED SOLUTION

Chris D
Kilo Sage
Kilo Sage

You don't need a script - you can use the ootb Flow Action Send Email or Send Notification. - should be completely no code if you like. In your flow, add the "Action" node > Flow Action > Spoke = ServiceNow Core > Action = Send Email | Send Notification.

The difference between the two is that Send Email is simpler and lets you craft an email from scratch, unique to this one use, whereas Send Notification requires you to first have created a "Triggered" Notification (sysevent_email_action), which has all the details defined in that record.

Generally I'd recommend using notifications rather than sending emails directly (they're better design-wise, reusable, in a standard location, and users can unsubscribe from them), but I can see Send Email being useful for you here so it's up to you.

ChrisD_0-1710420753805.png

 

View solution in original post

9 REPLIES 9

Oh, huh... that's a bummer. So I guess no-code is out of the picture then for Send Email. You could still do Send Notification no-code technically, but you certainly wouldn't be including any details from the conversation so that's out of the picture.

I'm guessing this is related to/because VA Designer doesn't support HTML/Rich Text inputs for topic blocks - though ironic because it does support HTML outputs and has a built-in HTML control for it.

 

So unfortunately, your only option it seems is low-code 😕

And in fact, if you want to associate the email to a record (a good idea imo, especially given email ACLs are dependent on it), it looks like the same needs to be done for the Target Record and Table inputs. I'd recommend associating the email to the Interaction record. So Table should be scripted, but a simple one-line return 'interaction'; and the Target Record script is also a one-liner: return vaVars._interaction_record_sysid;

 

Back to the Body input, since you can only use one Script Variable to define it, I don't see a point in using that - just use the Script button on the input and you'll need that script to:

//at the simplest
return '[insert html code here]';

//or some may prefer setting it as one or more variables first, like
var html = '[insert html code here]';
var html2 = '[more html]';
return html + html2;

Since you want parts of the email to be dynamic, the second option will be slightly cleaner.

Unfortunately, unless you're using a custom app with ES6 enabled, putting the HTML script and dynamic variables is gonna be way clunkier than it would be in modern code with Template Strings/Template Literals (i.e. the last section of that tutorial).

If you're not comfortable writing HTML code from scratch - or honestly, even if you are, to avoid manual mistakes - you can just craft your email in any WYSIWYG editor (like a KB article draft : ), then hit the code button to get the system-generated code to copy/paste.

Hey Chris, thanks for helping me understand this. I'm following along for the most part, but I'm not quite grasping how I can grab the user responses into the email body with HTML and scripting.

 

Could it be as 'simple' as something like this?

(function execute() {
var input = 'location'
var input1 = 'issue_details'
return '<p>Hi there</p>
<p>Please action the request using the details submitted below:</p>
<p>Location: 'location'</p>
<p>Issue details: 'issue_details'</p>
<p>Thank you,</p>';
})()

 My only hurdle with the above is capturing the input/user responses within the HTML.

That's more or less what it'll be like, with a few caveats.

You get the user input via vaInputs - not what you're doing in lines 2-3 I presume - so, let's say you have a reference or choice input "Location" and a text input "Issue Details", you'll get those using vaInputs.location.getDisplayValue() and vaInputs.issue_details (you shouldn't need to getDisplayValue() on a text field but test it out - sometimes you'll need toString() it, i.e. vaInputs.issueDetails.toString() - tbh I always forget what needs to be stringified).

 

Other key notes about your code: you're missing semi-colons at the ends of the lines (just good practice, not technically needed in javascript) but more importantly, the structure of your return string is wrong. Since you can't use Template Literals (designated by backticks, not single quotes), you're going to need to concatenate the strings making up your HTML line-by-line  (otherwise it'll treat each line separately - see point about semi-colons not needed : )

Here's an example of how it can look:

(function execute() {
  return '<p>Hi there</p>'
    + '<p>Please action the request using the details submitted below:</p>'
    + '<p>Location: ' + vaInputs.location.getDisplayValue() + '</p>'
    + '<p>Issue details: ' + vaInputs.issue_details.toString() + '</p>'
    + '<p>Thank you,</p>';
})()

Hope that helps! If this email example is close to your actual usage, it sounds like you'd be better off creating a record (i.e. Incident or Requested Item) and generating a normal Notification off the record creation instead of within the VA topic. It's technically more work and arguably more complex, but it's a better design - just something to think about.

Thanks Chris, you've been really helpful. I'm brand new to Javascript and this level of detail so it's a bit of an adventure. I'm still getting issues/errors so I think I'll need to go back to our in-house developers for the gritty details.

Feel free to share you code here if you get stumped - there's lots of little things that can go wrong - capitalization, mismatching quotes, etc, etc...

Once you get more the hang of it, even basic Javascript will be invaluable for VA development. You can see how vaInputs are used here and then you'll eventually find a use for vaVars, which are local variables you can use across the topic, set by code (not user input). And then arguably even more important - useful across the entire system, not just VA - is learning GlideRecord queries. Between those three things and core JS fundamentals (W3Schools is a great resource), you can do so much more with VA 😄