Jira Behaviours - Initialiser Function overwrites field content

Greg Hoggarth March 2, 2015

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

0 votes
Answer accepted
JamieA
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.
March 2, 2015

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.

Greg Hoggarth March 4, 2015

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