function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
buggs sfdcbuggs sfdc 

Slack and Salesforce Integration

HI Experts,

Here am trying to post a message in to slack when the opportunity stage changes,for these a i follwed below steps.
I tried all the way with process builder and Apex trigger but some how am not failed to post the opportuity stages messages on slack.
Please help me out!

Step 1: Create a Webhook

User-added image

Step 2: created a apex class:--
 
public with sharing class SlackOpportunityPublisher {
     
    private static final String slackURL = 'YOUR_WEBHOOK_URL';
     
    public class Oppty {
        @InvocableVariable(label='Opportunity Name')
        public String opptyName;
        @InvocableVariable(label='Stage')
        public String stage;
    }
     
    @InvocableMethod(label='Post to Slack')
    public static void postToSlack(List<Oppty> oppties) {
        Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'The following opportunity has changed:\n' + o.opptyName + '\nNew Stage: *' + o.stage + '*');
        msg.put('mrkdwn', true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public QueueableSlackCall(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();
            HttpResponse res = http.send(req);
        }
 
    }
    
}

Step 3:-- Created a process Builder:-- 
creiteria :-- When record is created or edited 

User-added image

Or Step 3 :-- i thought some problem with process builder and written a trigger on opportunity.
trigger opptyTrigger on Opportunity (after insert,after update) {
List<SlackOpportunityPublisher.oppty> solutions = new List<SlackOpportunityPublisher.oppty>();
    for (Opportunity sob : Trigger.new) {
        if(sob.Name != null  || sob.Name != ''){
        SlackOpportunityPublisher.oppty s = new SlackOpportunityPublisher.oppty();
        s.opptyName = sob.Name;
        s.stage = sob.StageName;
        solutions.add(s);
    }	
        }
    SlackOpportunityPublisher.postToSlack(solutions);

}
Thanks In Advance!
Madan Khadka 2Madan Khadka 2
@buggs sfdc : Hope this link will help you https://developer.salesforce.com/blogs/developer-relations/2016/05/slack-salesforce-integration.html