Amit Gujarathi
Giga Sage
Giga Sage

ServiceNow Slack Integration to Send Slack Messages

 

Hi All,
In this article we will look into how we can integrate ServiceNow to Slack. We have a usecase to send Salck Notification from ServiceNow whenever the record matches any given condition.

 

  • Create a Rest Message for the Slack API , configuration for which are as given below 

Screenshot 2024-02-23 at 2.22.48 PM.png

 

  • Create a Rest Resource for the same and provide the slack Authorisation details as highlighted on the screenshot
  • Screenshot 2024-02-23 at 2.26.07 PM.png

 

Copy the preview script and Generate a Script include for the sameScreenshot 2024-02-23 at 2.32.02 PM.png

  •  Script in the Script include as given below :
    var SlackNotificationUtils = Class.create();
    SlackNotificationUtils.prototype = {
    	initialize: function() {
    	},
    
    	sendSlackNotification : function (channelId,slackMessage) {
    		if (gs.nil(channelId)) {
    			throw 'Channel Id is empty';
    		}
    		var payload =
    			{
    				"channel": channelId,
    				"text": slackMessage
    			};
    
    		try { 
    			var r = new sn_ws.RESTMessageV2('Slack Notification for GDPR', 'Post Slack');
    			r.setRequestBody(JSON.stringify(payload));
    			var response = r.execute();
    			var responseBody = response.getBody();
    			var httpStatus = response.getStatusCode();
    			var obj = {
    				'responseBody' : responseBody,
    				'httpStatus' : httpStatus
    			};
    			
    			return JSON.stringify(obj);
    		}
    		catch(ex) {
    			var message = ex.message;
    			return JSON.stringify(message);
    		}
    	},
    	type: 'SlackNotificationUtils'
    };​
  • Now we are done with the slack set up
  • You can trigger the message to slack using below code for any table and any Slack Channel.
  • Syntax for trigger is as given below :
 var channelId = <Target Slack Channel name>;

    var slackMessage = <Message you wanna share to Slack Channel>;

    var response = new SlackNotificationUtils().sendSlackNotification(channelId, slackMessage);

 

Comments
paragkanoje
Tera Contributor

Hello @Amit Gujarathi ,

We also have alternate option like below:

If your instance has IntegrationHub and the Slack Spoke, you can automate Slack messages easily:

  1. Enable Slack Spoke in your ServiceNow instance (requires IntegrationHub).
  2. Create a Flow in Flow Designer.
  3. Use the “Post Message” action from the Slack Spoke.
  4. Configure the action:
    • Choose the Slack connection
    • Select the channel or user
    • Write the message text
  5. Add trigger conditions (e.g., incident created, updated, etc.).
  6. Test the flow to confirm messages are being sent.


    !!!!!!!!! Don't Heisted to mark helpful if this is expected......

 

Laveena_A
Tera Contributor

Hi @paragkanoje  

Tried through Integration Hub, it is so easy to configure and it works smooth like a butter 🙂

Version history
Last update:
‎02-23-2024 01:06 AM
Updated by:
Contributors