Send ServiceNow Surveys to Microsoft teams chat

Aravinda YS
Tera Contributor

Gathering employee feedback through internal surveys is crucial. But maximising response rates can be a challenge. This article explores how instant messaging (IM) (Microsoft Teams) can be a powerful tool for boosting survey participation than traditional Email channel.

AravindaYS_0-1713844623899.png

Solution Design

  1. Record update action will trigger survey assessment based on the trigger condition in ServiceNow
  2. ServiceNow flow will call MS power automate flow to send survey assessment card to the user in MS teams chat
  3. User responds with survey answers , Power automate flow call ServiceNow web service to update the survey answers
  1. AravindaYS_1-1713844948391.png

 

Implementation steps:

ServiceNow side
Step 1:

- Create a 'Survey' in survey designer and Survey trigger condition.
- View your survey and note down the Questions (asmt_metric) sys_ids , These values are required to update survey answers
Ex:

AravindaYS_3-1713846998013.png

 

Step 2:

Create a Flow

  • Trigger condition (same as survey trigger condition on current record)
  • Lookup Record action - to get the triggered survey assessment instance sys_id
    Table : asmt_assessment_instance
    Condition: Trigger_id = current record sysID
    AravindaYS_2-1713846225540.png

     

  • Create a flow action - This action will send current task record data and sys_id of assessment instance created to Microsoft Power automate flow.
    - create inputs to the flow action from flow (Task record and Assessment instance record from above action)
    -create JSON payload to for request body for power automation flow 
    Ex: I am passing story record information like below
    AravindaYS_5-1713847600890.png

    - Create a script action - to call Microsoft Power automate flow web service (api will be generated by power automate flow  'When an HTTP request is received' trigger used - Refer Step 4)

    AravindaYS_6-1713847789843.png

    Step 3:

    Create a Scripted REST API with 'PATCH' HTTP method resource. -  This will will receive survey response from MS power automation flow , we need to parse the request body to update the survey responses in the assessment questions.
    Expected request payload for this function be like below.

 

{
"assessinsta": "assessment insstance ID", \\ Assessment instance sys_id
"srvyResponse": {
"99cfa3cf1b1d8ad467882060604bcb45": "Survey answer1",\\ metric sys_id of the question noted in the STEP 1
"f1cfa3cf1b1d8ad467882060604bcbdb": "Survey  answer2",
"69cfa3cf1b1d8ad467882060604bcb8c": "Survey  answer3",
"bdcfe3cf1b1d8ad467882060604bcb0a": "Survey  answer4"
                            }
}

REST API function script

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 // implement resource here
var reqBdy = request.body.dataString;
var repObj = JSON.parse(reqBdy);
    //parse body and update assessment questions
    for (key in repObj.srvyResponse) {

      var qrString = "instance=" + repObj.assessinsta + "^metric =" + key;
        var grAAIQ = new GlideRecord('asmt_assessment_instance_question');
        grAAIQ.addEncodedQuery(qrString);
        grAAIQ.query();
        if (grAAIQ.next()) {
            if (grAAIQ.metric.datatype == 'string') {
                grAAIQ.string_value = repObj.srvyResponse[key];
            }
            grAAIQ.value = repObj.srvyResponse[key];
            grAAIQ.update();

        }
    }
	//complete assessment instance
	var grAAI = new GlideRecord('asmt_assessment_instance');
	grAAI.get(repObj.asin);
	grAAI.state = 'complete';
	grAAI.update();

 var result = {};
        result.message = 'updated in assessment :';
        return result;
})(request, response);​

 

Above 3 steps will complete servicenow side tasks


Microsoft Power Automate platform 

Spoiler
For servicenow developers with flow designer experience can comfortably create flows in Power automate platform.

Step 4:
Create a flow 
AravindaYS_7-1713849923575.png

    • Add the following elements:
      • Trigger:
        • Choose When an HTTP request is received.
        • Configure the trigger to accept POST requests and define a schema that matches the JSON payload structure from ServiceNow's Flow action (refer to step 3).
          AravindaYS_10-1713850680945.png

           

      • Adaptive Card:
        • Use Adaptive Cards Designer: https://adaptivecards.io/designer/ to create a card that presents the survey questions to the user.
        • Ensure the card's response schema aligns with the expected format for updating ServiceNow assessment questions (Refer Step 1).
          Ex: Question answer values should match the card answer values

      • Post to a chat or channel: (Optional)
        • Use this action to send the Adaptive Card to the caller's chat for interaction.
        • Extract the recipient's email from the trigger's output (if applicable).
          AravindaYS_9-1713850452082.png

           

      • Parse JSON:
        • Parse the user's responses from the Adaptive Card using the Parse JSON action.
        • Extract the relevant answer data using a content expression like body('your_adaptive_card_name')['data'].
      • HTTP request:
        • Call the ServiceNow Scripted REST API using the following details:
          • Method: POST
          • URI: The URI of your Scripted REST API function (refer to step 3)
          • Headers: Include an Authorization header with the value x-sn-apikey: <your_API_token> (replace with your generated API token)
            Spoiler
            Servicenow instance should be in Washington DC version ,To generate REST API token based authentication
            Alternatively you can use authorisation header with encode userid/password - use Postman tool to generated encoded string
          • Body: Request Payload same as Step 3 

AravindaYS_11-1713850905740.png

 

Add your questions in the comments , hit like if it is helpful for you.
Thanks - Aravinda

8 REPLIES 8

Kee5
Tera Contributor

Hello Aravinda, Thanks for the details. I have one issue during step 3. I have encounter below error

Error: Bad implementaion of call as constructor, name=RESTResponseV2 in org.mozilla.javascript.FunctionObject

 

I have the same script as you written above

Kee5_0-1721754288417.png

 

 

And this is the step detail:

Kee5_1-1721754454261.png

 

Appreciate if you can give me an insights what should be modified or fix.

g k1
Tera Guru

can you share the steps in detail?
i am not able to configure this

Thanks

gcasewell
Mega Contributor

This is EXACTLY what I am looking to do but having never done a lot of this in ServiceNow or Power Automate before I need more detailed examples. This guide is not quite granular enough. Are you still active on this platform? Would you be able to assist me by sharing more screenshots of the setup?

Hi @gcasewell ,
I am happy to help. Please reply with your question. Which step do you need clarification on?