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
Giussep EstradaGiussep Estrada 

Slack Integration With InvocableMethod

Hi
I'm trying to integrate Salesforce with Slack and use InvocableMethod to enter some parameters on Process Builder 

Right now in the Process Builder I can only put the Message that is sent to slack but I also want to give the option to put the Webhook URL, right now the URL is set on the APEX Class but I want to give this option on the Process Builder so the admins can set this without going to the APEX class


I create and InvocableVariable but I don't know how to use it to send the Slack Weebhook URL
It's my first time working with InvocableMethods, any help would appreciated it :D
Here is my code so far:
public with sharing class SlackPublisher {
    private static final String slackURL = 'WEBHOOKURL';
    
    public class MessagePost {
        @InvocableVariable(label='Slack Message')
        public String postText;
        @InvocableVariable(label='Slack Webhook')
        public String slackWebhook;
    }
    
    @InvocableMethod(label='Post Simple Message to Slack')
    public static void postToSlack(List<MessagePost> msgs) {
        MessagePost p = msgs[0];   
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', p.postText);
        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);
        }
    }
}

 
Vishwajeet kumarVishwajeet kumar
Hello,
Invocable method can accept List of Primitive or List of Custom objects as parameters. You can pass Url in a List<String>.

Something like this : 

@InvocableMethod(label='Post Simple Message to Slack')
public static void postToSlack(List<MessagePost> msgs, List<String> p_SlackUrls) {
   MessagePost p = msgs[0];
   String v_SlackUrl = p_SlackUrls[0];

   Map<String,Object> msg = new Map<String,Object>();
   msg.put('text', p.postText); msg.put('mrkdwn', true);
   String body = JSON.serialize(msg);
  System.enqueueJob(new QueueableSlackCall(v_SlackUrl , 'POST', body));
}


Thanks