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.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.