function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Satish Kumar LalamSatish Kumar Lalam 

I am working on Integration from JIra to Salesforce and vice versa. Now, How do we built custom Rest API for getting issue comment body(Richtext/htmlbody) and comment attachments from jira issue to salesforce case Feeditem record?

I could able to implement the custom Rest API to receive the text only format form Jira issue comment to case feeditem record. which is working fine with the below code.  

@RestResource(urlMapping='/api/webhooks/pushDetails/*')
global class jiraWebhookListner {
    @HttpPost
    global static Void receiveJiraIssueComments(){
        
        system.debug('API Request--> ' +RestContext.request);
        
        system.debug('API Request Body --> ' +RestContext.request.requestBody); 
        
        RestRequest request = RestContext.request;
        
        Blob body = request.requestBody;
        
        //string requestBody = request.requestBody.toString();
        String bodyString = body.toString();
        
        system.debug('String body = '+bodyString);
        
        Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(bodyString);
        
        system.debug('payload = ' +payload);
        
        string commentText = (String)payload.get('comment');
        string issueKey = (String)payload.get('issueKey');
        
        system.debug('Data display = ' + (string)payload.get('Data'));
        system.debug('commentText = ' + commentText);
        system.debug('issueKey = ' + issueKey);
        
        case caseObj = [Select Id from case WHERE SL_JIRA_Key__c = : issueKey LIMIT 1];
        
        system.debug('case = ' + caseObj);
        
        IF(caseObj != null){
            FeedItem feedItemObj = new FeedItem();
            feedItemObj.IsRichText = True;
            feedItemObj.ParentId = caseObj.Id;
            feedItemObj.Body = commentText;
            insert feedItemObj;
            
            RestContext.response.statuscode = 200;
            RestContext.response.responseBody = Blob.valueOf('Comment added successfully');
        } else{
            RestContext.response.statuscode = 404;
            RestContext.response.responseBody = Blob.valueOf('Case not found for issue key: ' + issueKey);
        }
        
    }

}

Now I would like to send RichText/Html body like text having BOLD, Italic style...etc. along with attachments. Kindly advice on how to acheive this.

Also, Below is the code that I can able to send only the text body of FeedItem of case object to Jira Issue comment.

 @future(callout=true)
    public static void createJiraComment(Set<Id> FeedItemIds){  
        FeedItemtinsert = true;            
        List<FeedItem> FeedCommentList = [select id, ParentId, Body, CreatedById from FeedItem where id IN: FeedItemIds AND CreatedById !='005S000000RstyT    '];
        If(FeedCommentList!=null){
            Case PerentCase = [Select Id, SL_JIRA_Key__c from case where Id =: FeedCommentList[0].ParentId ];   
            User cmntUser = [Select Id, Name FROM User where Id =: FeedCommentList[0].CreatedById];
            string Username = cmntUser.Name;
            
            String endpointUrl='https://sureify.atlassian.net/rest/api/3/issue/'+PerentCase.SL_JIRA_Key__c+ '/comment';
            If(FeedCommentList[0].Body!=null){
                for(FeedItem FeedCommentRec : FeedCommentList){
                    string cmntbody = 'Commented by ' + cmntUser.Name +' : ' + FeedCommentRec.Body;
                    system.debug('FeedCommentRec.Body == ' + FeedCommentRec.Body);
                    Http http = new Http();
                    HttpRequest request = new HttpRequest();
                    request.setEndpoint(endpointUrl);
                    request.setMethod('POST');
                    
                    request.setHeader('Content-Type', 'application/json');
                    
                    Blob headerValue = Blob.valueOf('jira-intg-user@sureify.com' + ':' + Label.Jira_Int_Token);
                    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
                    request.setHeader('Authorization', authorizationHeader);
                    
                    
                    String body='{"body":{"content":[{"content":[{"text": "'+cmntbody+'","type":"text"}],"type": "paragraph"}],"type": "doc","version": 1}}';
                    
                    request.setBody(body);
                    
                    HttpResponse response = http.send(request);
                    system.debug('----response--'+response);
                    system.debug('---response body--'+response.getBody());
                    if (response.getStatusCode() == 201){
                        
                        system.debug('---couponResults--'+response.getStatusCode());
                        
                    }
                }
            }
        }
    } 
    
}

I would like to send RichText/Html  FeedItem body like text having BOLD, Italic style...etc. along with attachments to JIra Issue comments. Kindly advice on how to acheive this.
SubratSubrat (Salesforce Developers) 
Hello Satish ,

To send rich text/HTML body and attachments from Salesforce Case FeedItem records to Jira Issue comments, you need to make some modifications to your code. Here's an approach you can follow:

Handle Rich Text/HTML Body:
Modify the createJiraComment method to include the rich text formatting in the comment body.
Use the Jira Comment REST API to send the rich text content. Instead of sending plain text, you can structure the body as HTML.
Ensure that you properly escape any special characters in the HTML content to avoid issues during serialization and transmission. Salesforce provides the String.escapeHTML4() method that you can use for this purpose.
Here's an example of how you can modify your code to include rich text formatting:
String cmntbody = 'Commented by ' + cmntUser.Name + ': <b>Bold Text</b> <i>Italic Text</i>';
String escapedCmntBody = String.escapeHtml4(cmntbody);
String body = '{"body":{"type": "html", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": "' + escapedCmntBody + '"}]}]}}';
Handle Attachments:
To include attachments from Salesforce Case FeedItem records in the Jira Issue comments, you need to fetch the attachments associated with the FeedItem.
Iterate over the attachments and upload them to Jira using Jira's attachment REST API. You will need to make a separate API call for each attachment.
Include the attachment URLs or IDs in the comment body along with the rich text content.
Here's an example of how you can handle attachments:
List<FeedAttachment> attachments = [SELECT Id, ParentId, Name, Body FROM FeedAttachment WHERE FeedItemId IN :FeedItemIds];

for (FeedAttachment attachment : attachments) {
    // Upload attachment to Jira and obtain the attachment URL or ID
    String attachmentUrl = uploadAttachmentToJira(attachment.Name, attachment.Body);
    
    // Include the attachment URL or ID in the comment body
    body += 'Attachment: ' + attachmentUrl + '<br>';
}
Note: The uploadAttachmentToJira method is not provided in the code snippet above. You would need to implement it to handle the actual upload of attachments to Jira.

By following this approach, you can send rich text/HTML body and attachments from Salesforce Case FeedItem records to Jira Issue comments. Remember to handle the serialization and deserialization of data correctly based on the API requirements of Jira.

Hope this helps !
Thank you.
Satish Kumar LalamSatish Kumar Lalam
Hi Subrat,
Thanks for the response. I am getting the error saying 'Method does not exist or incorrect signature: void escapeHtml4(String) from the type String' when use the below method.
String escapedCmntBody = String.escapeHtml4(cmntbody);