Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Post Function to Update Reporter Field

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

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?

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!

 

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

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.

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

413 views 2 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you