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

0 votes

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,021 views 12 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot