{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":9733,"path":"employee-service-management/emergency-outreach/create-emer-outreach-notif-channel.md","title":"Create an Emergency Outreach notification channel","bundle":null,"doc_type":null,"release":"australia","body":"\n# Create an Emergency Outreach notification channel\n\nCreate a notification channel to send health status requests using your company's preferred communication method, such as the Slack or Microsoft Teams collaboration platforms. Create the notification channel using a script or a subflow.\n\n## Before you begin\n\nRole required: sn\\_imt\\_checkin.checkin\\_admin or admin\n\n## About this task\n\nAfter you create a notification channel, you can select the channel when sending a notification. The channel uses the specified subject and body, and sends notifications to the users that you select when you send the health status request.\n\nFor example, your company's IT network group has a Slack channel that they monitor more closely than email. Create a Slack notification channel. To communicate with IT network group members, select the group as the target audience and select the Slack notification channel.\n\nIf the **Email notification** and **Now Mobile Push notification** check boxes are selected, all three notifications are sent. To use only the Slack notification channel, clear these two check boxes.\n\n## Procedure\n\n1.  Navigate to **All** &gt; **Emergency Outreach** &gt; **Channels**.\n\n2.  Click **New**.\n\n    \\[Omitted image \"emer-outreach-notif-channel.png\"\\] Alt text: New channel record with the script option selected and default script.\n\n3.  Enter the name of your notification channel.\n\n    Use a short name that indicates the communication method to make it easy for the requester to pick the correct channel.\n\n4.  In the **Subject** field, enter the subject of your notification channel.\n\n5.  In the **Body** field, enter the body message.\n\n6.  Create a script notification channel.\n\n    1.  In the **Notification type** field, leave the default notification type of **Script**.\n\n    2.  In the **Script** field, replace the code in the comment with the script to connect to your channel.\n\n        ```\n        {\n                    // sendNotification(responses.acknowledgementsGr.user.email, \n        channelGr.subject, channelGr.body);\n                }\n        ```\n\n        For information about JavaScript APIs for GlideRecord, see [GlideRecord - Scoped](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_GlideRecordScopedAPI.md).\n\n    3.  Click **Submit**.\n\n    The following example includes code to count the number of acknowledgements and survey instances generated when the notification is sent. It displays the number of acknowledgements and survey instances that succeeded. Use this information to review and handle errors that appear in the Delivery Log related list in the Outreach record.\n\n    ```\n    (function notify(responses, channelGr) {\n        var record = responses.mode === 'acknowledgements' \n            ? responses.acknowledgementsGr \n            : responses.surveyInstancesGr;\n        var totalCount = record.getRowCount();\n        var numSent = 0;\n        while (record.next()) {\n            try {\n                // sendNotification(record.user.email, channelGr.subject, channelGr.body)\n                numSent += 1;\n            } catch (err) {\n                gs.error('Error occurred: ' + err.message);\n            }\n        }\n        return { total_count: totalCount, sent_count: numSent };\n    })(responses, channelGr);\n    ```\n\n7.  Create a subflow notification channel.\n\n    1.  Create a subflow to use in the outreach notification.\n\n        For information about creating, publishing, and testing subflows, see [Subflows](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/build-workflows/subflows.md).\n\n        Use the following inputs in the subflow that you create:\n\n        -   *mode* \\(string\\)\n        -   *akcnowledgements* \\(Records of sn\\_imt\\_checkin\\_check\\_in\\_acknowledgement\\)\n\n            If the mode is *acknowledgements*, then acknowledgements are set to a valid GlideRecord and *survey\\_instances* is `null`.\n\n        -   *survey\\_instances* \\(Records of asmt\\_assessment\\_instance\\)\n\n            If the mode is *survey\\_instances*, then survey\\_instances is set to a valid GlideRecord and *acknowledgements* is `null`.\n\n        -   *channel* \\(Reference of sn\\_imt\\_checkin\\_channels\\)\n        The following outputs are optional:\n\n        -   *total\\_count* \\(integer\\)\n        -   *sent\\_count* \\(integer\\)\n        To review these outputs, include the number of total acknowledgements or surveys versus the successful acknowledgements or surveys.\n\n    2.  Select the **Subflow** notification type.\n\n        The Script section changes to Flow, and the **Subflow** reference field appears.\n\n    3.  Select the subflow that you created.\n\n8.  Click **Submit**.\n\n\nWhen an outreach is sent using a custom notification channel, a Delivery Logs related list appears in the Outreach message.\n\n\\[Omitted image \"outreach-delivery-log-list.png\"\\] Alt text: Delivery logs related list showing a failed log status.\n\n1.  Test the notification channel for an outreach message, and open the failed record to review the error message.\n2.  Fix errors that caused the notification to fail.\n3.  Continue testing the notification channel until it no longer fails.\n\n## Slack channel script\n\n**Note:** The following script is for reference only. Refer to it when you develop a notification channel script for your environment.\n\n```\n(function notify(responses, channelGr) {\n    var createClient = function () {\n        var client = new sn_ws.RESTMessageV2();      \n        client.setRequestHeader('Authorization', 'Bearer xoxb-222222222222-1111111111111-000000000000000000000000');\n        client.setRequestHeader(\"Accept\", \"application/json\");\n        client.setRequestHeader('Content-type', 'application/json');\n        return client;\n    };\n\n    var getEmailToSlackIdMap = function (users) {\n        var emailToSlackIdMap = {};\n        for (var i = 0; i < users.length; i++) {\n            var email = users[i].profile.email;\n            if (email) {\n                emailToSlackIdMap[email] = users[i].id;\n            }\n        }\n        return emailToSlackIdMap;\n    };\n\n    var runSlackCommand = function (method, params) {\n        var client = createClient();\n        client.setHttpMethod('post');\n        client.setEndpoint('https://slack.com/api/' + method);\n        client.setRequestBody(JSON.stringify(params));\n        var response = client.execute();\n        if (response.getStatusCode() < 200 || response.getStatusCode() > 299) {\n            throw new Error('Failure running ' + method + ':\\n' + response.getBody());\n        }\n\n        var body = JSON.parse(response.getBody());\n        if (!body.ok) {\n            throw new Error(body.error);\n        }\n\n        return body;\n    };\n\n    var sendMessage = function (userId, message) {\n        var openConversationResponse = runSlackCommand('conversations.open', { users: userId });\n        var channel = openConversationResponse.channel.id;\n        runSlackCommand('chat.postMessage', { channel: channel, text: message });\n    };\n\n    var users = runSlackCommand('users.list').members;\n    var emailToSlackIdMap = getEmailToSlackIdMap(users);\n    var body = channelGr.getValue('body') || '<p></p>';\n    var record = responses.mode === 'acknowledgements' ? responses.acknowledgementsGr : responses.surveyInstancesGr;\n    var numSent = 0;\n\n    while (record.next()) {\n        var email = record.user.email + '';\n\n        var link = responses.mode === 'acknowledgements'\n            ? gs.getProperty('glide.servlet.uri') + record.getLink(true)\n            : gs.getProperty('glide.servlet.uri') + 'sp?id=take_survey&instance_id=' + record.getUniqueValue();\n\n        var message = body + '\\n' + link;\n\n        var slackId = emailToSlackIdMap[email];\n        if (slackId) {\n            sendMessage(slackId, message);\n            numSent += 1;\n        }\n    }\n\n    return { sent_count: numSent };\n})(responses, channelGr);\n```\n\n"}}