Jira Behaviours - Initialiser Function overwrites field content

I've set up the Initialiser Function so that two fields, Contact Person and Contact Email, are automatically filled in with the logged-in user's name and email address. Often they will want to keep these values, but sometimes they'll want to change them, when recording an issue on behalf of an external 3rd party for example.

The initialiser function seemed like the ideal place to do this, and the code for it is very easy.

It all works fine when an issue is Created (and therefore the fields are initially empty), but when clicking Edit, it replaces any content in the field with the current user name. This is annoying if person A creates the record, then person B goes to edit it - suddenly person A's details are replaced by person B's, which is not what we want at all.

I tried adding a check so that the field population would only happen if the field content was already "", this solves the edit problem but now when you create an issue for the first time, the fields aren't filled in at all. I tried checking against null, but in that case the field is always updated, even when clicking edit, which also seems wrong.

Here's my current (non-working) validation script:

import com.atlassian.jira.ComponentManager
def user = ComponentManager.getInstance().getJiraAuthenticationContext().getUser()
FormField person = getFieldByName("Contact Person")
FormField email = getFieldByName("Contact Email")
if (person.getValue() == "")
    person.setFormValue (user.getDisplayName())
    email.setFormValue (user.getEmailAddress())

Contact Person and Contact Email are just plain single-line text fields.

Does anyone have a suggestion to get this working?

Maybe these Initialiser functions need to have a Conditions option like regular field mappings do, then I could make it fire for the Create screen only?

1 answer

1 accepted

It depends what you want... you can make your code fire only on Create by doing:

if (getActionName() == "Create Issue") {...}

but check that is the correct action name for your workflow.

That's probably the best approach. 

Your code is not working because getValue will return null rather than an empty string, you could better write it:

if (person.getValue())

PS, initialisers function is the right place for tasks like this.

Would it be sensible to add the extra Conditions for these initialiser functions? That way people could easily restrict the behaviour without having to add this sort of stuff into their script.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,655 views 18 21
Read article

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