Scriptrunner - How to add user as mention in issue comment via post function

Marianne Lee (Nagarro) October 3, 2018

I'm new to Scriptrunner & groovy scripts, and I'd appreciate help & advice.

I have a Jira customfield of user-picker type, called "Process Manager", and I would like to add the "Process Manager" user as a user-mention in a issue comment triggered during a transition in workflow.

The results I keep getting is of string type (plain text) or null, unlike the assignee which is displayed as a user mention in the comment and thus getting notified by email.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.opensymphony.workflow.WorkflowContext


def customFieldManager = ComponentAccessor.getCustomFieldManager();
UserManager userManager = ComponentAccessor.getUserManager();

def processmgrCF = customFieldManager.getCustomFieldObjectByName('Process Manager');
def processmgr = issue.getCustomFieldValue(processmgrCF);

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();

def fullText = "... by ${processmgr} and [~${issue.assignee?.key}]"

ComponentAccessor.getCommentManager().create(issue as Issue, userManager.getUserByName(currentUser), fullText as String, true);

How do I show the custom field as a user object type in a comment, like the assignee?

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
October 5, 2018

Hi Marianne,

A user-picker type custom field contains the same ApplicationUser type object as your Assignee field. With that in mind, you can mention a user in your custom field the very same way you do it with assignee. Something like this should do the trick:

def fullText = "... by [${processmgr?.key}] and [~${issue.assignee?.key}]"

 

Marianne Lee (Nagarro) October 12, 2018

Hi Ivan,

[${processmgr?.key}] didn't work for me, but following your suggestion and line of thought I modified it to  [~${processmgr.name}] and that works.

def processmgrCF = customFieldManager.getCustomFieldObjectByName('Process Manager');
String processmgrName = issue.getCustomFieldValue(processmgrCF).name;
ApplicationUser processmgr = ComponentAccessor.getUserManager().getUserByName(processmgrName);

def fullText = "...by [~${processmgr.name}] og [~${issue.assignee?.key}]"

 

Thanks for your guidance!

Like slyphox likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events