1

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.

1 Answer 1

1

Your trigger is passing an array of Solution SObjects to the postToSlack method while that method is expecting an array of SlackSolutionPublisher.solution objects. Change your trigger to convert the SObjects to those simple Apex objects and then make the call.

(Or perhaps cleaner to provide an additional method in SlackSolutionPublisher that accepts the SObject array.)

The insert trigger will work in calling postToSlack every time a new Solution SObject is created. But you should think about supporting the bulk case. On the Apex side that might mean using a Batchable as no more than 100 requests can be made in one transaction. Don't know about possible problems in Slack (and from a quick look don't see bulk operations in that API that would simplify the problem).

PS

The code to convert would look something like this:

trigger solutionTrigger on Solution (after insert) {
    SlackSolutionPublisher.Solution[] solutions = new SlackSolutionPublisher.Solution[] {}
    for (Solution sob : Trigger.new) {
        SlackSolutionPublisher.Solution s = new SlackSolutionPublisher.Solution();
        s.solutionTitle = sob.SolutionName;
        s.status = sob.Status;
        solutions.add(s);
    }
    SlackSolutionPublisher.postToSlack(solutions);
}
3
  • Sorry for the question, but how would I convert the SObjects to simple Apex objects? Or provide an additional method in SlackSolutionPublisher? I am already reaching as far as my Apex knowledge goes, and I am realizing I am in WAY over my head. Really appreciate the help! Aug 3, 2016 at 10:28
  • @MisterMalik Added to the answer.
    – Keith C
    Aug 3, 2016 at 10:45
  • That worked, great! I only had to delete the line of s.status. Thank you very much! Aug 3, 2016 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.