Forums

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

Scriptrunner - Add user mention (from custom field) within a comment

Tyler Reum January 25, 2019

Hello,

I am attempting to set up an escalation service that will automatically ping users who have not approved tickets after a period of time.

I've browsed a number of existing community postings, however there's always some minor variation that doesn't really allow for a clean copy & translation to my use case...  plus my lack of experience doesn't help.

Here's the code I'm using:

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 ApprovalCF = customFieldManager.getCustomFieldObjectByName('Approval Required By');
String approvalName = issue.getCustomFieldValue(ApprovalCF);

def comment = "This purchase request is still pending and required approval from [~${approvalName}]"

issueInputParameters.setComment(comment)

It is commenting & retrieving data from the custom field as expected, however this is how it's rendering in the comment:

This purchase request is still pending and required approval from [~treum(treum)]

What must I change in order for the script to actually mention the user properly?

Admittedly, I have very little experience with Scriptrunner/Groovy but I'm looking to learn and could use some help! Thanks in advance for any suggestions.

1 answer

0 votes
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.
January 25, 2019

Hi @Tyler Reum

Instead of:

[~${approvalName}]

 use:

[~${approvalName.getName()}]

 User picker type custom fields contain an ApplicationUser object. What you need to do is retrieve a username for that object and then pass it to your comment.

Hope this helps.

Tyler Reum January 25, 2019

Hi Ivan,

Thanks so much for the response.

So, do I need to then define the ApplicationUser object in order for this to work?

I tried replacing 

[~${approvalName}]

 with

[~${approvalName.getName()}]

as suggested, however now I receive the following error:

[Static type checking] - Cannot find matching method

java.lang.String#getName(). Please check if the declared type is correct

and if method exists.

Possible solutions: getClass(), getBytes(), getChars(), getAt(int),

getAt(groovy.lang.IntRange), getAt(java.lang.String) @ line 14, column 85.

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.
January 25, 2019

you can either ignore this error, it should work as is, or declare the correct type for your 'approvalName' variable:

ApplicationUser approvalName = issue.getCustomFieldValue(ApprovalCF);
Tyler Reum January 25, 2019

Interesting... I get no results when attempting to run the script while the error exists.

When I attempted to replace 

String approvalName = issue.getCustomFieldValue(ApprovalCF);

with 

ApplicationUser approvalName = issue.getCustomFieldValue(ApprovalCF);

I got a new error:

[Static type checking] - Cannot assign value of type java.lang.Object to

variable of type com.atlassian.jira.user.ApplicationUser @ line 12, column 32.

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.
January 26, 2019

Like I said, static type checking in scriptrunner is not always accurate as per this documentation and as such can be ignored most of the times.

As to the code itself, does it actually add a comment to your issue? Have you tried running it in script console? For scriptconsole you'll need to specify an issue, like so:

def issue = ComponentAccessor.getIssueManager().getIssueObject("ABC-1234")

Try adding some logs and see what they say:

log.error("Comment to add: ${comment}")

 

Tyler Reum February 27, 2019

I ended up getting this to work a different way.

I wanted additional actions based off the triggering of this Escalation Service, so I set it up to trigger a (hidden) transition instead of a simple comment.

When issues are transitioned there are a few post functions setup - one will trigger a webhook that ultimately sends a Slack message to the user, and the other is a comment inside the ticket that I was wanting.

It's not the cleanest, but using JMWE's Comment Issue post function it's working well!

<%def approvalRequiredBy = issue.get("customfield_12105")?.name%> 
<%="[~"+approvalRequiredBy+"]"%> this request has been inactive for at least three days, and has yet to be approved by you. Please approve or reject this issue by clicking the "Approve/Reject" transition button.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events