0

I'd like to apologize in advance as I'm quite new to writing APEX Classes / Triggers. I've followed a tutoriel of which the aim is to POST a comment to an external system, in this case it's Jira.

I have a few problems, first of all I'm thinking my IssueId should be something I define as a variable that I'd like to set from the flow that triggers the apex class. How do you think I should go about doing this ?

I'd also like to set the Body as a variable set in the triggering flow.

I suppose I'd have to write methods to do this sort of thing ?

Finally it seems this class is not invocable due to the datatype of the method. Perhaps writing new methods to support above requirements would fix my issue ?

I'd love to have a few pointers to help me go about this.

public class JIRAwebserviceCallout {
    public static void addCommentToJiraIssue(String IssueId){
        try{
            Jira_Credentials__c jiraCreds = [Select id, Jira_API_Token__c, Jira_Password__c, Jira_URL__c, Jira_UserName__c from Jira_Credentials__c Limit 1];
            HttpRequest req = new HttpRequest();
            HttpResponse res = new HttpResponse();
            Http http = new Http();
            Blob headerValue = Blob.valueOf(jiraCreds.Jira_UserName__c + ':' + jiraCreds.Jira_API_Token__c);
            String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('content-type','application/json');
            req.setHeader('accept', 'application/json');
            req.setMethod('POST');
            req.setEndpoint(jiraCreds.Jira_URL__c + '/rest/api/2/issue/' + IssueId + '/comment');
            req.setBody('{"body": "This is a test comment" }');
            res = http.send(req);
            System.debug('ResponseBody::'+res.getBody());
        }
        catch(Exception ex){
            System.debug('ERROR:' + ex.getMessage());
        }
    }
}
2

1 Answer 1

1

First of all, have you considered using an External Service, instead? External Services allow you to make direct API calls to other servers without any direct Apex code. Set up an Authorization Provider, Named Credential, and an External Service, and you can then directly access it from the Flow, without any code at all. You'll want to check out the JIRA OAuth 2.0 docs, and you'll need the link to the appropriate Swagger (this link) to create your External Service. While exactly zero code is necessary for this, it may actually take longer to set up initially than just using Apex, but you should at least know there's an option.

Also, I'd recommend against your current design anyways. The Jira_Credentials__c object is just duplicating effort that you can save with a Named Credential, you can read more about using them in Named Credentials as Callout Endpoints.

As far as the current question, Flows are made to be bulkified. This means that the input parameter must be a List, and the output parameter, if any, must also be a List. The number of output items must match the number of input items, and must be in the same order. This is why the compiler says that your code is incorrect. You'll need to start it off as:

public static void addCommentToJiraIssue(String[] IssueId){

Or, you may want to use an InvocableVariable instead:

public class JiraCommentRequest {
  @InvocableVariable String issueId;
  @InvocableVariable String comment;
}

Which you can then pass in:

public static void addCommentToJiraIssue(JiraCommentRequest[] requests) {

Remember that you'll need to loop over the requests and process each one. You might also need to go asynchronous if you already have "work pending", since you cannot make a callout after a DML.

1
  • Thank you so much for taking the time to respond !! This is perfect. I should be able to handle it from here :-)
    – Notacat9
    Jun 23 at 13:15

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.