This article is a supplement to the ServiceNow documentation. For full documentation please refer ServiceNow official website
Checkout our NEW Video Channel you can like and subscribe too!

Use Case: Whenever a new Sev 1 or Sev 2 case is created and assigned to TEST-CORE-UNIX, TEST-CORE-INTEL We have to get notified on slack channel #test-core-client-internal with below message

Example - Ticket number - Customer - Short Description - This “Severity” ticket is in queue, requires an owner

Slackusecase190120211.JPG

To Achieve this we are going to create a Business Rule

Name: Case notification for SQ_Core Groups

Table: Case

Slackusecase1901202115.JPG

Slackusecase1901202117.JPG

Slackusecase1901202118.JPG

var slackLinkByGroup = {};

//Slack-Group Mapping
slackLinkByGroup['TEST-CORE-SERVICES-INTERNAL'] = "https://hooks.slack.com/services/Tiiiijjjhhh/yBMNpZCkjhgfdsa";
slackLinkByGroup['TEST-CORE-CLIENT-INTERNAL'] = "https://hooks.slack.com/services/TS7lklhgffds/slqdioiuytredf883";

var coreServiceGroups = ["TEST-CORE-DATABASE", "TEST-CORE-Backup"];
var coreClientGroups = ["TEST-CORE-UNIX", "TEST-CORE-INTEL"];
var allowedInstances = ["https://dev567.service-now.com/"];
var instanceURL = gs.getProperty('glide.servlet.uri');

(function executeRule(current, previous /*null when async*/ ) {
    if (this.isAllowedInstance()) {
        var isPlainTextRequired = false;
        var emoji = "mega";
        var colorCode = "#36a64f";
        var headingText = "This Severity " + current.priority + " is in queue and requires an owner";
        var ticketNumber = current.number;
        var customer = current.account.getDisplayValue();
        var shortDescription = current.short_description;
        var incidentLink = instanceURL + "sn_customerservice_case.do?sys_id=" + current.sys_id;
        var fallBackText = ticketNumber + "-" + customer + "-" + shortDescription + "-" + headingText;
        var plainTextMessage = "{\"text\": \"" + fallBackText + "\"}";
        var slacklink = this.evaluateSlackLink(current.assignment_group.getDisplayValue());
        var assignmentGroup = current.assignment_group.getDisplayValue();

        var template = "{ \"attachments\":[";
        template = template + "{\"fallback\": \"%fallBackText%\",";
        template = template + "\"color\": \"%colorCode%\",";
        template = template + "\"fields\": [";
        template = template + "{\"title\": \":%emoji%: %headingText%\", \"short\": false },";
        template = template + "{\"value\": \"Ticket number : `%ticketNumber%`\", \"short\": false },";
        template = template + "{\"value\": \"Customer : `%customer%`\", \"short\": false },";
        template = template + "{\"value\": \"Assignment Group : `%assignmentGroup%`\", \"short\": false },";
        template = template + "{\"value\": \"Short Description: `%shortDescription%`\", \"short\": false },";
        template = template + "{\"value\": \"_Click here to view it_ : <%incidentLink%|*incident*>\",\"short\": false }";
        template = template + "]}";
        template = template + "]}";

        template = template.replace("%fallBackText%", fallBackText);
        template = template.replace("%colorCode%", colorCode);
        template = template.replace("%headingText%", headingText);
        template = template.replace("%ticketNumber%", ticketNumber);
        template = template.replace("%customer%", customer);
        template = template.replace("%assignmentGroup%", assignmentGroup);
        template = template.replace("%shortDescription%", shortDescription);
        template = template.replace("%incidentLink%", incidentLink);
        template = template.replace("%emoji%", emoji);

        var finalMessage = isPlainTextRequired ? plainTextMessage : template;

        try {
            var request = new sn_ws.RESTMessageV2();
            request.setHttpMethod('post');
            request.setRequestBody(finalMessage);
            request.setEndpoint(slacklink);
            var response = request.execute();
        } catch (ex) {
            //Nothing to do
        }
    }
	current.update();
})(current, previous);

function evaluateSlackLink(groupName) {
    if (coreServiceGroups.indexOf(groupName) !== -1) {
        return slackLinkByGroup['TEST-CORE-SERVICES-INTERNAL'];
    }
    if (coreClientGroups.indexOf(groupName) !== -1) {
        return slackLinkByGroup['TEST-CORE-CLIENT-INTERNAL'];
    }
}

function isAllowedInstance() {
    return allowedInstances.indexOf(instanceURL) !== -1;
}
    Content