Skip to main content
Florian Ganster ha preguntado en #Apex
Hi guys, 

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?

1 respuesta
0/9000