I have an Apex class used for posting messages from Salesforce to Slack everytime a new solution is created. I got it to work with Process Builder and opportunities, but Solutions is not supported in Process Builder.
I figured I would need to make a Trigger.
So I have the following Class:
public with sharing class SlackSolutionPublisher {
private static final String slackURL = 'WEBHOOK_URL' //Edited out for privacy
public class solution {
@InvocableVariable(label='Solution Title')
public String solutionTitle;
@InvocableVariable(label='Status')
public String status;
}
@InvocableMethod(label='Post to Slack')
public static void postToSlack(List<solution> solutions) {
solution o = solution[0]; // If bulk, only post first to avoid overloading Slack channel
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', 'A new solution has been created:\n' + o.solutionTitle + '\nNew status: *' + o.status + '*');
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);
}
}
}
And the following Apex trigger:
trigger solutionTrigger on Solution (after insert) {
Solution[] solutions = Trigger.new;
SlackSolutionPublisher.postToSlack(solutions);
}
However, I am getting the following error:
Method does not exist or incorrect signature: SlackSolutionPublisher.postToSlack(List<Solution>) at line 3 column 8
I'm guessing that it has something to do with the fact that triggers can’t use invocable methods?
How would I be able to set it up so that postToSlack runs everytime a new solution is created? Do I need to edit the Apex code, or look in a different direction entirely?
Any help is greatly appreciated.