Pre-populate custom field with "current user" value

Christian Czaia _Decadis AG_
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.
July 8, 2012

Hi everyone,

I've been looking everywhere to find a solution for my problem. I have a custom field (user picker) called "responsible" and I need it to be pre-populated with the "reporter" (or current user) value on issue creation. I can't just copy the value as part of a post-function since users should have the possibility to change the value right away (if they are not the person being responsible)

There used to be a plugin addressing the problem which seems to outdated.

https://jira.atlassian.com/browse/JRA-7947?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aworklog-tabpanel

I've been trying to achieve this with the behaviours plugin but with no success.

FormField formAutor = getFieldByID("reporter")
FormField formAG = getFieldByName("Auftraggeber")


if(formAG.getFormValue() == ""){

 formAG.setFormValue(formAutor.getFormValue())
 
}

Any clues or is this a real piece of work? If yes, I have to check with my customer if this request is a must-have...

Thank you,

Christian

7 answers

1 accepted

0 votes
Answer accepted
Mizan
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.
July 8, 2012

This should be possible using Behaviours plugin .

Try the below script , even add some logging (you will have to click on enable logging on the behaviours plugin )

FormField formAutor = getFieldByID("reporter")
FormField formAG = getFieldByName("Auftraggeber")
 
 
if(!formAG.getFormValue()){
 
 formAG.setFormValue(formAutor.getFormValue())
  
}

Christian Czaia _Decadis AG_
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.
July 8, 2012

Do I have to active another logging behaviour (set a logger to debug?)

Mizan
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.
July 8, 2012

yes there is a enable logging link on the behaviours page

Christian Czaia _Decadis AG_
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.
July 8, 2012

I know, just wanted to check if I had to set a JIRA logger to "debug" instead of warning in order to see the entries in the log file.

Mizan
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.
July 8, 2012

Ok , so is the script working ?

Christian Czaia _Decadis AG_
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.
July 8, 2012

Hey, it's not working. Can't view the log right now since the server admin has already gone home:-) I even tried without the if clause with no result. The value doesn't get copied.

Christian Czaia _Decadis AG_
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.
July 9, 2012

Any chance of retrieving the current reporter (or current user) without using the field "reporter" on my issue create screen? My customer doesn't want the creator field being displayed...

Mizan
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.
July 9, 2012

yes you can use the following line

User currentUser = componentManager.getJiraAuthenticationContext().getUser()

Christian Czaia _Decadis AG_
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.
July 10, 2012

I've achieved it by using:

import com.atlassian.jira.ComponentManager

FormField formAG = getFieldByName("Auftraggeber")       

currUser = ComponentManager.getInstance().getJiraAuthenticationContext().getUser()
currUserName = currUser.getName()

if(!formAG.getFormValue()){     
 
 formAG.setFormValue(currUserName) 
 }

Mizan
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.
July 10, 2012

Great :)

Hey can you try the below code .. it wont make much difference

import com.atlassian.jira.ComponentManager
 
FormField formAG = getFieldByName("Auftraggeber")      
 

 
if(!formAG.getFormValue()){    
formAG.setFormValue(ComponentManager.getInstance().getJiraAuthenticationContext().getUser().getName())
 }

0 votes
Asha_Mariswamy
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!
May 26, 2021

@Christian Czaia _Decadis AG_ i just updated the code to latest classes and it worked

 

Thanks alot!!

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FormField

FormField formAG = getFieldByName("latest user")

if(!formAG.getFormValue()){
formAG.setFormValue(ComponentAccessor.getJiraAuthenticationContext().getUser().getName())
}

0 votes
Lubomir Styskala April 6, 2018

Hello,

can someone advise me where to get Behaviours plugin, please? I have looked up on Atlassian marketplace but with no success. Nothing like Behaviours plugin for Jira.

Thank you in advance,

L.

Nati Levi April 28, 2018

It's part of the ScriptRunner plugin

Lubomir Styskala May 3, 2018

Thank you, it helped.

0 votes
JIRA_USER April 26, 2017

Hi,

I have almost similar issue. Could someone please help me.

My issue is, I have a custom field and a system field (Reporter). I would have a value in my custom field and I want that value to auto populate to the Reporter field if that user(value) listed in custom field maches to the user list in Reporter field.

How do I do this?

0 votes
Melanie Rogers July 30, 2015

The script worked for me also in the Behaviours plugin. In addition, I would like to choose a different user in the custom field (user picker) if it is not the Reporter. Is there a simple "else" statement I could use?

0 votes
Christian Czaia _Decadis AG_
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.
July 8, 2012

Got a typo:

FormField formAutor = getFieldById("reporter")
FormField formAG = getFieldByName("Auftraggeber")
if(!formAG.getFormValue()){
formAG.setFormValue(formAutor.getFormValue())
}

I had ID :-)

0 votes
Alex Suslin
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.
July 8, 2012

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events