
Trying to create an apex class in order to receive a notification in Slack when a New Event with the Subject "Demo" is created.
public class NewDemoBooked {
private static final String slackURL = 'https://hooks.slack.com/services...';
public class Event {
@InvocableVariable(label='Subject')
public String Demo;
}
@InvocableMethod(label='Post to Slack')
public static void postToSlack(List<Event> events) {
Event o = events[0]; // If bulk, only post first to avoid overloading Slack channel
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', 'The following event has been created: \nSubject: *' + o.Demo + '*');
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);
}
}
}
I've got a code coverage of 10%
Can anyone help me?
Hi Florian,Greetings!Can you please check the sample class for http Post request:https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.htmlKindly let me know if it helps you and close your query by marking it as best Answer so that it can help others in the future.Warm Regards,Shirisha Pathuri