Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner Post Function to Update Reporter Field

BH
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 12, 2022

Hi All -

 

We have been using an old version of JSU to copy a value from a text field (email address) to the Reporter Field. This means that the Reporter can be an email address instead of a user.  I.e. "example@atlassian.com". Newer versions of JSU do not allow this feature and the old version of JSU still works but who knows for how long.

As a result, we have tried getting ScriptRunner to do this for us and have had no had luck. We are on the latest versions of ScriptRunner and JIRA Server. I have tried doing this with a post function and listener. I saw that the copy field value option does not include the  Reporter field. Perhaps because it is a system field?

Trying old code examples does not work. Does anyone have any ideas?

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Radek Dostál
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 12, 2022

You cannot store String value in a field expecting ApplicationUser. You're actually corrupting your database and it will eventually bite you back, I'm surprised as to how you were able to do so before and how invalid your jiraissue table is at this point. (Edit: I think I might have misunderstood your post, and the JSU function was storing the user, not the email string?)

 

What would be the correct way to do so would be to take the email address, find the user with this address, and save that user as the Reporter (and I could put together a snippet for that if you're not sure how to do it). Is there anything that you are doing with the Reporter afterward, why this isn't the option, what was the reason to store email instead of proper user in the first place?

Brett Haberle
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 12, 2022

Hi Radek,

 

What you are suggesting would work for us as well. The problem we are encountering is if a user tries to make a ticket using one of our special JIRA urls it is not capturing their information. Users without an account can also use these shortcut ticket creation URLs. When users use these urls it shows the reporter as "Anonymous".  This prevents them from getting the email alerts.

 

I will play with the login settings to see and get back to you. Thank you!

 

Radek Dostál
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 12, 2022

Okay, so I did a proof of concept that works on the Create (well it should in any transition):

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.impl.GenericTextCFType
import com.atlassian.jira.bc.user.search.UserSearchService
import com.google.common.base.Strings
import com.atlassian.jira.user.ApplicationUser

final String TEXTFIELD_NAME = "Email Text Field"

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField textFieldCF = customFieldManager.getCustomFieldObjectsByName(TEXTFIELD_NAME).find {
it.getCustomFieldType() instanceof GenericTextCFType
}

if (textFieldCF == null) {
log.error("'" + TEXTFIELD_NAME + "' Custom Field not found on the system!")
return
}

String emailValue = issue.getCustomFieldValue(textFieldCF)
if (Strings.isNullOrEmpty(emailValue) || !emailValue.contains('@')) return //no value or not an email

UserSearchService userSearchService = ComponentAccessor.getUserSearchService()

ApplicationUser userMatchedByEmail = null

Iterable usersByEmail = userSearchService.findUsersByEmail(emailValue.trim())
Iterator iterator = usersByEmail.iterator();
while (iterator.hasNext()) {
userMatchedByEmail = iterator.next();
} // this should ideally ONLY EVER find a single user, otherwise up to you whether to use the last one or do nothing

if (userMatchedByEmail != null) {
MutableIssue mutableIssue = (MutableIssue) issue;
mutableIssue.setReporter(userMatchedByEmail);
}
else {
//no user found, do nothing? Or add a comment to issue to notify the assignee, or..
}

 

Note that the post-function order will matter:

pic.png

Note I added extra 'Stores updates' function (this is out of box). I haven't tried it without it, but if memory serves it's needed, alternatively we could do the update in the script too, but.. the less the better.

 

I'm working here on the assumption that:

 - the user in that email is not interacting with Jira, they are using some other portal?

 - then this other portal takes their details, and it creates an issue in a project (probably some functional user doing that?)

 

In any case, if I've misunderstood some part, please let me know, I'm really a little thick today, not the brightest day.

Like BH likes this
BH
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 12, 2022

Thanks. I was able to run this without having to add stores updates to an issue and it worked.

 

The only problem we have is if the email address is not recognized then it defaults to "Anonymous". We would prefer it just shows their email address. But to your point, if that is a nono then we will try to force account creation before the user submits their ticket.

0 votes
Kristin Lyons
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 12, 2022

Just to better understand - is there a specific reason for using the email address instead of the username?  You might be able to use Automation for Jira to do this but I would have to test it out in my environment first.

TAGS
AUG Leaders

Atlassian Community Events