Script listener to add comment

Sharadkumar Bangera November 24, 2020

Hello,

 

i m trying to add comment in Jira  whenever priorities changes but endup with error.

Please help me to correct my script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.issue.priority.Priority
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.Issue
event.issue.issueType.name == 'Incident'

def CustomFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find
def comment = event.comment?.body
{it.field == "priority"}
if (change)

{commentManager.create("Value changed from ${change.oldstring} to ${change.newstring}")}
//{log.warn "Priority Changed"}
//{log.warn "Value changed from ${change.oldstring} to ${change.newstring}"}
//else
//{log.warn "Priority remain same"}

 

1 answer

1 accepted

1 vote
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2020

Hi @Sharadkumar Bangera ,

please try something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

CommentManager commentManager = ComponentAccessor.getCommentManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == PRIORITY}
if (change) {
commentManager.create(event.issue, event.user, "Value changed from ${change.oldstring} to ${change.newstring}", false)
}
Sharadkumar Bangera November 26, 2020

Hi Hana,

 

Thanks a ton.

My script is working as expected..I was struggling for from last few days to get the desired result and you appeard as angel with the miracle script.

 

Thanks a lot.

Like Hana Kučerová likes this
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2020

Thanks, you are very kind, I'm glad to help :-)

Sharadkumar Bangera November 27, 2020

Hi @Hana Kučerová ,

 

Sorry to bother you one more time.

The script is working as expected but it shows my name whenever the priority is changed

Sharadkumar Kariya Bangera 27/Nov/20
Priority changed from Minor to Trivial

I am looking this should show Service Desk Automation user

Priority changed from Minor to Trivial 27/Nov/20

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2020

Hi @Sharadkumar Bangera ,

please try to modify it like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

CommentManager commentManager = ComponentAccessor.getCommentManager()
ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey("username")
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == PRIORITY}
if (change) {
commentManager.create(event.issue, user, "Value changed from ${change.oldstring} to ${change.newstring}", false)
}

Just replace "username" with the username of your automation user.

This automation user needs to have set of permissions for your project like Browse Projects and Add Comments.

Sharadkumar Bangera November 27, 2020

Thanks @Hana Kučerová for your help.

I tried your script but it still reflects my name instead of "ServicedeskAutomation".

i change the username to "ServicedeskAutomation"(this is how it is created in User Management)

ServicedeskAutomation have Administer Permission to projects

 

What i m looking is

Service Desk Automation

Priority changed from Minor to Major

 

At Present it appear as

Sharadkumar Kariya Bangera 27/Nov/20
Priority changed from Minor to Trivial

Sharadkumar Bangera November 27, 2020

script.PNG

Sharadkumar Bangera November 27, 2020

sorry , My bad.i over-read your script , made changes on last line ".It is working as expected but the comment appears as "internal" and i want this to be public

commentManager.create(event.issue, user, 

"instead of

commentManager.create(event.issue, event.user, 
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2020

Hi @Sharadkumar Bangera ,

I believe your automation user needs to have Service Desk license, to be able to communicate with your customers. Would you please check?

Sharadkumar Bangera November 30, 2020

Hi Hana,

 

Thanks for your assistance.

I will check as per your comment.

 

Thanks once again.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events