How to add value to custom field

Hello dear gurus :)

Is there a way to set value of a custom field based on other field? I know it's possible but i need to add value, not to replace it with new one. For example - a field that stores all usernames who become assignees of the issue.

1 answer

1 accepted

0 vote

What code are you proposing to use to do this?

It will need to read the current multi-user picker contents and add the new one to the list.

I was trying to use something like scripted field from Groovy Runner (there is an example in comments of https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted+Fields#ScriptedFields-Aninformationmessagebut it refers to select list from linked issues and i was not able to change the code for my case).

Ok, so what have you got in your code so far? Just the bit that tries to read the current values, build the new set and post it back?

This is the code I got:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
def componentManager = ComponentManager.getInstance()
def executioners = ""
def cf = issue.assigneeUser
executioners += getCustomFieldValue(cf) + " "

But it returns [null]. I see that my code is wrong but I don't get how to add value to the field. I tried to change field type from "Text Searcher" to "User Picker" - of course it shows [Anonymous]

It's not the searcher, that only defines how it's indexed and searched, and you can't just "change field type".

I am assuming that

  • You have a field called "executioners"
  • The executioners field is a multi-user-picker
  • You are trying to add the current assignee of the issue to the executioners field

Your code is broadly correct until the very last line. That line won't work because it's a simple "concatenate" or "add" operation and you're not working with strings or numbers. You're actually working with a user object and an array of users from the multiselect.

Executioners should be set by saying something like "executioners = getCustomFieldValue(cf).add(issue.assigneeUser)"

("Executioners" is an interesting choice of words by the way. It is correct, but in English, is only really used to describe "a person who legally kills criminals on the behalf of the government", we rarely use it to say "someone who did something", even though we do say "this procedure was executed")

Thank you very much for your answers, Nic! Still having problems... The "Executioners" field is not a multi-user-picker - it's a Scripted Field. I tried your suggestion:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
def componentManager = ComponentManager.getInstance()
def executioners = ""
def cf = issue.assigneeUser
executioners = getCustomFieldValue(cf).add(issue.assigneeUser)

It returns this error:

javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method add() on null object

I guess that a have to set a default value for this field but I don't get how it's possible... I tried to define:

def executioners = issue.assigneeUser

but still get same error

I know that "executioner" is not the fully correct name for this field but my imagination insisted on it - I can't find other word for it ^^ But I'll have to search dictionaries of synonims to get the name - I don't think that my superiors will understand any humour in the system

Ah, ok, a scripted field is totally different.

A scripted field can only return data to be displayed to the user when they visit an issue. Basically, you can't add anything to it at all, because it's not really storing data, it's working it out on the fly.

Very simply, forget the "getCustomFieldValue" part of the code - there's no value in a scripted field. All your code can do with that type of field is determine the output. The best I can do here is simplify your code down to something that will work, but probably will NOT do what you're trying to achieve.

(after all the imports and definitions of functions you might use)

executioners = issue.assigneeUser().getName() ;

return executioners ;

So my code transformed into this:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
def componentManager = ComponentManager.getInstance()
def executioners = ""
executioners = issue.assigneeUser.getName()
return executioners

It returns [Anonymous] but it's not the main problem. As I can understand this type of field can't store its value - so it will just show me the current assignee and not the list of previous assignees. I guess it's the wrong way of solving the problem and I will try to use database trigger. If you are interested in the solution, I'll post it when it's done. Thank you very much for your help though!

NO!

DO NOT USE TRIGGERS!

You must NEVER write to a Jira database while Jira is running, and even if you do it when it's stopped running, you have to re-index after restarting it (and that assumes your SQL is safe)

So, having yelled that, could you explain your actual requirement? Now we know you can't quite get there with the scripted field as above, we should step back and look at the actual need.

The requirement is like this:

I need to get the list of every assignee of the Issue in a single field. The type of field doesn't matters - it can be text or user picker, no difference.

Why I should not use triggers? I already have one that is running for about 3 or 4 months... It doesn't write into database - it updates one value of a field that corresponds to a determined issue - and it works fine, the changes are shown right in the moment that the event occurs. Can you specify the danger of using triggers? The database has reindexing job that runs every hour.

What do you mean "it doesn't write - it updates one value" - that IS a database write and it's almost certainly screwing your Jira installation. You need to shut it down immediately, remove the triggers, restart Jira and reindex it.

If you make changes to Jira data that Jira is unaware of, you can be corrupting caches, the internal index (not the database index), temporary files and memory. The damage can leach into your database and render it unusable even if you clean it up.

Please. Stop doing that. I've spent a good chunk of my career battling with Jira installations that have been damaged by database writes.

OK, i will do that. Thank you for your warning

Ok, once you've undone that damage, I think I understand what you need.

You've got two options here

1. Add a multi-user-picker custom field, and write a listener (you can do this in the script runner). A listener can look for changes on issues, and when it spots a change of assignee, it can read the multi-user picker field for the current data and add the new assignee in

2. Write a scripted field that reads the history of the issue, pulls out all the "assignee changed FROM" fields and puts them into a display string

The advantage of the first option over the second is that it will show the users as actual users, not just a simple string. The downside is that it will only work from the point at which you install it (the scripted field approach will work after you re-index)

*facepalm* Why I didn't think about script listener... Thank you very much!

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

246 views 1 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you