Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Woz
Tera Contributor

The Requirement 

Today we were presented the challenge of:

"We want an MS Teams notification sent to every member of an assignment group whenever a new incident is assigned to that group"


I will probably write a separate post about the investigation into that but during the investigation into it I discovered something I thought was really cool.

Sending a MS Team message from a script 


ServiceNow has an API for firing teams messages that can be called form a custom flow action, business rule, wherever you like!
The Script include is called MSTeamsOutboundMessages but unfortunately its locked down. But looking into some other scipts I found that MSTeamsOutboundMessages().sendDirectMessage() takes three arguments:

  •  sys_id of the user who should receive the teams message
  •  the string "adaptiveCard"
  • a JSON which is what tells teams what to display. The JSON is part of "Adaptive Cards"  which you can read all about here https://adaptivecards.io/.   There is even a GUI designer for creating cards found here https://adaptivecards.io/designer/


Here is an example: 

var inc_number = "INC007"
var send_to = '00004934db486850e2bd49a239961949'
var priority = "P1"
var SD = "Short Description"
var record_id = "000024e1be38d906da0a798bd4bcbe9"

var card =
{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "size": "Large",
            "weight": "Bolder",
            "text": "Incident has been assinged to your queue.\n ",
            "wrap": true
        },
        {
            "type": "TextBlock",
            "size": "Medium",
            "weight": "Bolder",
            "text": priority + " " + inc_number,
            "wrap": true
        },
        {
            "type": "TextBlock",
            "text": SD,
            "wrap": true
        },
        {
            "type": "ActionSet",
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "title": "View Ticket details",
                    "url": "https://xxxx.service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D" + record_id
                }
            ]
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3"
}

var outBoundMessage = new MSTeamsOutboundMessages();
var response = outBoundMessage.sendDirectMessage(send_to, MSTeamsConstants.OUTBOUND_MESSAGE_TYPE.ADAPTIVE_CARD, card2);


We can create a card that looks like

find_real_file.png

 

Doing something useful with that information

Now we know how to send cards we can create a custom Flow Action

Inputs 
find_real_file.png

 

Then we add a scripted step

find_real_file.png

We need to add our input values 
find_real_file.png

And our script

(function execute(inputs, outputs) {
var card =
{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "size": "Large",
            "weight": "Bolder",
            "text": "Incident has been assigned to your queue.\n ",
            "wrap": true
        },
        {
            "type": "TextBlock",
            "size": "Medium",
            "weight": "Bolder",
            "text": inputs.number + " \n " + inputs.priority,
            "wrap": true
        },
        {
            "type": "TextBlock",
            "text": inputs.short_description,
            "wrap": true
        },
        {
            "type": "ActionSet",
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "title": "View Ticket details",
                    "url": "https://xxxxx.service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D" + inputs.record_id
                }
            ]
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3"
}

var outBoundMessage = new MSTeamsOutboundMessages();
var response = outBoundMessage.sendDirectMessage(inputs.user_id, MSTeamsConstants.OUTBOUND_MESSAGE_TYPE.ADAPTIVE_CARD, card);
  
})(inputs, outputs);

Then we can test using find_real_file.png

find_real_file.png

 

An providing the the user in the "send to id" field has their account linked to MS Teams they should get a card

find_real_file.png

Comments
Ashley
Kilo Sage

Good Afternoon,

I'm giving this a go as its a great idea but I'm getting an error in flow designer:

find_real_file.pngHere's the last bit of the code:

var outBoundMessage = new MSTeamsOutboundMessages();
var response = outBoundMessage.sendDirectMessage(inputs.caller, MSTeamsConstants.OUTBOUND_MESSAGE_TYPE.ADAPTIVE_CARD, card)

Any ideas?

Kind Regards

Ashley

Ashley
Kilo Sage

Managed to resolve it, I created the Flow in the Global Scope instead of the "Microsoft Integrations - Core" Scope... doh

Now just to figure out how to get the response back

Kind Regards

Ashley

Suraj Ranabhat
Tera Contributor

Here we are sending it to a single user using "inputs.caller" and hardcoded sysId.

How can we send to all members of the assignment group? 

Woz
Tera Contributor

@Suraj Ranabhat - if you create a flow action to create the card that takes a sys_id of an input.

The have a flow that loops through all the users of a group and calls that action for each user on every iteration 

jnpetersen
Tera Contributor

Is there a specific plugin that needs to be installed to find the "MSTeamsOutboundMessages" script include?  I don't appear to have it in my instance.

Ashley
Kilo Sage

Hi @jnpetersen 

 

That script include is part of the "Microsoft Integrations - Core" plugin.

 

If open the plugin, scroll to the bottom and open the related list "Installed Files", a quick search in the Display Name field will find it for you.

 

Hope it helps

 

Kind Regards

 

Ashley

Mukul_Kolhe88
Tera Contributor

Could anyone please help me to understand where to write this above script and some background of it. Actually we have implemented Out of the box MS Teams Servicenow integration and now we need to show more info on Adaptive card when Approval is requested. can someone please help on this issue. 

ChrisUSC84
Tera Contributor

Is it possible to do this from the task table so all assignments are sent this way?  We have a similar requirement for when a ticket is assigned from any record type to the assigned_to.

Subham_
Tera Contributor

Hi Woz,

 

How to implement using virtual agent?

 

Thanks & Regards 

Shubham

 

Version history
Last update:
‎05-25-2022 11:54 AM
Updated by: