You need to sign in to do that
Don't have an account?
Hermann Ouré
Apex class to Send Slack Notification to multiple channels
Hello,
I have an Apex class sending slack notification to a channel and I would like to update the code to send the notification to multiple slack channels.
Is Anyone familiar with Slack integration?
Here is my code
I have an Apex class sending slack notification to a channel and I would like to update the code to send the notification to multiple slack channels.
Is Anyone familiar with Slack integration?
Here is my code
public without sharing class SlackNotificationSupport{ public class slackRequest { @InvocableVariable(label='caseNumber') public String caseNumber; @InvocableVariable(label='status') public String status; @InvocableVariable(label='nickName') public String nickname; @InvocableVariable(label='queue') public String queue; @InvocableVariable(label='accountTier') public String accountTier; @InvocableVariable(label='accountName') public String accountName; @InvocableVariable(label='subject') public String subject; @InvocableVariable(label='businessUnit') public String businessUnit; } public static String setChannelName(String queue) { String channelName; channelName = '#'+queue; channelName = channelName.toLowerCase('en'); channelName = channelName.replaceAll('queue', 'bot'); channelName = channelName.replaceAll('[_]', '-'); return channelName; } @InvocableMethod(label='Publish to Slack') public static void publishToSlack(List<slackRequest> requests) { String webhookURL = system.label.Param_Slack_Token; String msg; String channelName; for(slackRequest r:requests){ if (r.queue == 'internal'){ System.debug('### SlackNotificationSupport new internal case'); channelName = '#'+Label.Slack_Internal_Case_Channel; msg = 'A new internal case has been created : *'+r.caseNumber+'* - By User : (*'+r.accountName+'*) - Subject : (*'+r.subject+'*)'; } else if (r.queue == 'caseconcern'){ System.debug('### SlackNotificationSupport new case concern'); channelName = '#'+Label.Slack_Case_Concern_Channel; msg = 'A new Case Concern has been created : *'+r.caseNumber+'* - By User : (*'+r.nickName+' '+r.accountName+'* From *'+r.accountTier+'*) - Category : (*'+r.subject+'*)'; msg += '\nLink to Case Concern : '+URL.getOrgDomainUrl().toExternalForm()+'/'+r.status; } // Team Leads else if (r.queue == 'Queue Team Leads') { channelName = setChannelName(r.queue); msg = 'A customer has opened a new case.\n>>>*'+ r.caseNumber+'* - '+r.subject; System.debug('### SlackNotificationSupport Queue Team Leads'); } // New Tier 1 Ticker else if (r.accountTier == 'Tier 1' && r.accountName != null && r.queue != null) { System.debug('### SlackNotificationSupport New Tier 1'); channelName = setChannelName(r.queue); msg = 'The customer '+r.accountTier+' - *'+r.accountName+'* has opened a new case.\n>>>*'+ r.caseNumber+'* - '+r.subject; }// Assigned ticket, status to Open - Internal notification for awaiting feedback cases else if (r.nickname != null && r.status != null && r.caseNumber != null) { System.debug('### SlackNotificationSupport Status x to Open'); channelName = '@'+r.nickname; if(r.queue == 'internal_notification')msg = 'Salesforce Internal Case number *'+r.caseNumber+'* related to : *'+r.subject+'* - Status changed to : *'+r.status+'*.'; else msg = 'Case number *'+r.caseNumber+'* has become '+r.status+'.'; } // Generate JSON for request try { if (r.queue != null || r.nickname != null) { System.debug('### SlackNotificationSupport Sending message'); JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); //Inserts { gen.writeStringField('text', msg); gen.writeStringField('channel', channelName); gen.writeStringField('username', 'bot-support'); gen.writeStringField('icon_emoji', ':smartplus:'); gen.writeEndObject(); //Inserts } String body = gen.getAsString(); //Translates JSONGenerator to string to be passed to callout System.debug('### SlackNotificationSupport body: '+ body); System.enqueueJob(new qCallOut(webhookURL, 'POST', body)); // Send request } else { System.debug('### SlackNotificationSupport Queue = '+ r.queue); return; } } // try catch (exception e){ system.debug('### SlackNotificationSupport error:' + e); } } } public class qCallOut implements System.Queueable, Database.AllowsCallouts { private final String url; private final String method; private final String body; public qCallOut(String url, String method, String body) { this.url = url; this.method = method; this.body = body; } public void execute(System.QueueableContext ctx) { HttpRequest req = new HttpRequest(); req.setEndpoint(url); req.setMethod(method); req.setBody(body); Http http = new Http(); // to pass when process builder is invoked by another test class if(!Test.isRunningTest()){ HttpResponse res = http.send(req); } } } }