Sync user mentions in comments from Jira Cloud to Salesforce

Originally asked by Syed Majid Hassan on 14 October 2022 (original question)


We were recently requested by a client to provide scripts which would sync user mentions from Jira Cloud comments to Salesforce chatter feed comments.


Answer by Syed Majid Hassan on 14 October 2022

There are many approaches that can be taken to solve this use case, but we used the following in this case:

Step 1: Read the mention in the Jira comment and replace it with the users email address

Jira Outgoing Script Expand source

replica.comments = issue.comments.collect {
    comment ->
    def matcher  = comment.body =~ /\[~accountid:([\w:-]+)\]/
    def newCommentBody = comment.body
    matcher.each {
        target = nodeHelper.getUser(it[1])?.email
        newCommentBody = newCommentBody.replace(it[0],target)
    }
 
    comment.body = newCommentBody
    comment
}

Step 2: Find the email address from replica.comments and call a Salesforce endpoint to add the correct user mention to that comment

Salesforce Incoming Script Expand source

def commentMap = [
   "mathieu.lepoutre@idalko.com" : "0057Q000006fOOTQA2",
   "syed.majid.hassan@idalko.com" : "0057Q000006fOOOQA2"
   ]
replica.comments.collect {
    comment ->
    def matcher  = comment.body =~ /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/
    def newCommentBody = comment.body
    matcher.each {
        newCommentBody = newCommentBody.replace(it[0],"")
        def res = httpClient.post("/services/data/v54.0/chatter/feed-elements", \
        "{\"body\":{\"messageSegments\":[{\"type\":\"Text\", \"text\":\"${newCommentBody} \" },{\"type\":\"Mention\", \"id\":\"${commentMap[it[0]]}\"}]},\"feedElementType\":\"FeedItem\",\"subjectId\":\"${entity.Id}\"}")
    }
}

The commentMap above is needed to correlate the Salesforce accountId to the incoming email address. This can be replaced if you have the same email address configured on both ends and are willing to make some effort looking for the correct account on Salesforce account programmatically.

A video demonstration of the scripts in action is attached here:

Thanks
Majid


A file named ‘attachment_58495594_unknown-attachment’ was present in the original content but couldn’t be uploaded due to unsupported file type.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.