Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Adding user from user picker to comment

Per Kafka Thygesen February 8, 2019

Hi!

I am trying to add a automaticlly generated comment on an issue when it is transitioned. The comment should "tag/reference" a user that is stored in a single select user picker.

I almost got this working, but i am missing the last step.

My user is returned as a string and not an object. In the format "username(username)".

Is there a way to get the plain username?

And is it at all possible to add a comment that references a user? (Like adding a @username manually).

My post function script looks like this:

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// Get the current Issue
def issue = issue;

// Get the Manager classes required
def commentManager = ComponentAccessor.getCommentManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();

//Get the tester user from customfield
def customField = customFieldManager.getCustomFieldObjectByName("Reviewer"); //customfield_10304
if(customField) {
String user = issue.getCustomFieldValue(customField);

if(user) {
def CommentText = "Review requested @${user}";
commentManager.create(issue,CurrentUser,CommentText,true);
}
}

 

Thanks in advance! 

1 answer

1 accepted

0 votes
Answer accepted
Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2019

Hi,

Custom field Reviewer has ApplicationUser type. But getCustomFieldValue method is general so it returns Object type. It's possible to cast returned value to ApplicationUser. And also mention user syntax is a bit different: [~${user.name}]. So the whole script could look like:

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// Get the current Issue
def issue = issue;

// Get the Manager classes required
def commentManager = ComponentAccessor.getCommentManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();

//Get the tester user from customfield
def customField = customFieldManager.getCustomFieldObjectByName("Reviewer"); //customfield_10304
if(customField) {
def user = issue.getCustomFieldValue(customField) as ApplicationUser;

if(user) {
def CommentText = "Review requested [~${user.name}]";
commentManager.create(issue,CurrentUser,CommentText,true);
}
}

Suggest an answer

Log in or Sign up to answer