I am working on a solution to send a message to a Slack channel when a user creates a record on Salesforce. I have gone through Apex SDK for Slack App and managed to connect the app with Salesforce.
I want to create a trigger on Salesforce so that, when a user creates a record, it will invoke Slack.ActionDispatcher class:
/*
This example apex class extends Slack.ActionDispatcher and is responsible for
responding to the "onsubmit" event defined in the 'view_opportuniy', 'view_account',
and 'view_contact' view.
*/
public class ActionDispatcherPostRecordDetailMessage extends Slack.ActionDispatcher {
public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
return Slack.ActionHandler.ack(new Handler(parameters, context));
}
public class Handler implements Slack.RunnableHandler {
Map<String, Object> parameters;
Slack.RequestContext context;
public Handler(Map<String, Object> parameters, Slack.RequestContext context) {
this.parameters = parameters;
this.context = context;
}
public void run() {
Slack.App app = Slack.App.ApexSlackApp.get();
Slack.UserClient userClient = app.getUserClientForTeam(this.context.getTeamId(), this.context.getUserId());
String recordId = (String) this.parameters.get('recordId');
String objectApiName = (String) this.parameters.get('objectApiName');
Slack.ViewReference viewReference = Slack.View.record_detail_message.get();
viewReference.setParameter('headerText', objectApiName + ' Record Details');
viewReference.setParameter('recordId', recordId);
viewReference.setParameter('objectApiName', objectApiName);
Slack.ChatPostMessageRequest req = new Slack.ChatPostMessageRequest.builder()
.channel((String) this.parameters.get('channelId'))
//TODO should text be required if you are setting a view reference?
.text('Details for record id' + recordId)
.viewReference(viewReference)
.build();
Slack.ChatPostMessageResponse response = userClient.ChatPostMessage(req);
if (response.getError() != null) {
System.debug(response.getResponseMetadata().getMessages());
}
}
}
}
This is my trigger:
public static void handleOpportunity(List<Opportunity> newOpportunities) {
for (Opportunity opp : newOpportunities) {
ActionDispatcherPostRecordDetailMessage.Handler handler = new ActionDispatcherPostRecordDetailMessage.Handler(
new Map<String, Object>{
'recordId' => opp.Id,
'objectApiName' => 'Opportunity',
'channelId' => 'your_slack_channel_id_here'
},
new Slack.RequestContext()
);
handler.run();
}
}
In the trigger I get a Method is not visible: void Slack.RequestContext.<init>() error
I am expecting that, when a user creates a record, a Slack message is sent to a channel with the details of the record. After the record is in the channel, I will user a block it to update the record in Salesforce.