Set assignee to component lead unless previous assignee is not previous component lead?

I need to set the assignee to the component lead only if the previous assignee was the lead of the previous component. I've got a Behaviours plugin script to set the assignee to the component lead:

import com.atlassian.jira.ComponentManager

FormField formComponent = getFieldById(fieldChanged)
FormField formUserField = getFieldById("assignee")

if (formComponent.getValue() != null && formComponent.getValue() != "") {
    List components = formComponent.getValue() as List
 
    usera = components.first().lead

    formUserField.setFormValue(usera)
}

The plugin seems to act when a field changes. Can it know the pervious value of a field?

2 answers

0 votes

Yes, but you need to read the change history to find it - iterate over that looking for "your field" and oldvalue/newvalue

The problem is that the field will not have the changed value saved at that point. The behaviour runs every time the field is changed while the record is being edited, with seemingly no memory of what the field's original value was, but the change history only knows the old value once the record is saved.

0 votes

To get values of the issue as it is currently in the database, disregarding user input in the form, call methods on the underlyingIssue Issue object, which is available as a variable in your scripts.

So underlyingIssue.assignee should be the assignee as it was before the user started editing.

Spot on, as usual. Thanks! Here's the finished code:

import com.atlassian.jira.ComponentManager

FormField componentField = getFieldById(fieldChanged)
FormField assigneeField = getFieldById("assignee")

originalAssignee = underlyingIssue.assignee.getName()
originalComponentLead = underlyingIssue.components.first().lead

leadChange = (originalAssignee == originalComponentLead)

if (leadChange && componentField.getValue() != null && componentField.getValue() != "") {
    List components = componentField.getValue() as List
 
    newLead = components.first().lead

    if (newLead != null && newLead != "") {
       assigneeField.setFormValue(newLead)
    }
}

Good stuff.

underlyingIssue.components.first().lead can throw an NPE if there are no components... I would do some checking with that, unless you're sure the components can't be empty.

Ran into a bump. If there is no component originally specified, I want the project lead to act in the same way as the original component lead, i.e. be replaceable by the new component lead. The original case still works (replace component lead with component lead), but in the new case, the issue returns a "Validation error: Issues must be assigned" and the field doesn't get set.

import com.atlassian.jira.ComponentManager

// If the component changes and the issue is still assigned
// to the original component lead/project lead, change the assignee
// to the new component lead

FormField componentField = getFieldById(fieldChanged)

// Get original assignee

originalAssignee = underlyingIssue.assignee
if (originalAssignee != null && originalAssignee != "") {
 originalAssigneeName = originalAssignee.getName()
} else {
 originalAssigneeName = null
}

// Get original component lead
// If there are no components, get project lead

originalComponents = underlyingIssue.components
if (originalComponents != null && originalComponents != "" && ! originalComponents.empty) {
 originalComponentLead = originalComponents.first().lead
} else {
 originalComponentLead = underlyingIssue.project.lead
}

// If the assignee is not the component or project lead, don't update the assignee

leadChange = (originalAssigneeName == originalComponentLead)

// If there is a component and assignment is OK, chagne the assignee to the lead for the new component

if (leadChange && componentField.getValue() != null && componentField.getValue() != "") {
    List components = componentField.getValue() as List
 
    newLead = components.first().lead

    if (newLead != null && newLead != "") {
       FormField assigneeField = getFieldById("assignee")
       assigneeField.setFormValue(newLead)
    }
}

I can;t really debug all that code. So basically you need to just default the assignee to the project lead if there is no component set?

It's the same path as before, but with the project lead in the place of the lead for the original component:

Is there already a component?
-> Yes: Assignee = lead of original component?
---> Yes: Change to lead of new component
---> No: Do nothing

-> No: Assignee = project lead?
---> Yes: Change to lead of new component
---> No: Do nothing

The bolded branch is where the problem arises. Jira returns an "Issues must be assigned" error, even though the field value should be changed. All other branches work fine.

My original, pre-3.0.5 stab at this (changing the assignee) came up with the same error. Is there a better way to set the assignee than assignee.setFormValue?

Thanks again for all your help.

EDIT: the behaviour works when some users are set as assignee, but not all users. Even if there is no component, the behaviour will work for those users, but for other users, it never works, even if there is a component. The non-settable users can be manually set as assignee, just not via the behaviour.

I've compared the profiles of the users, and they don't seem to differ in a noticeaeble way. Very perplexing.

Is there a way to set the assignee via the underlyingIssue object?

So can we reduce it to the following: if you set the assignee through a behaviour, jira then says there is no assignee when you try to submit?

That is correct.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

22,254 views 2 7
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