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

Joe Mallon
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.
August 29, 2014

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
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.
August 31, 2014

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.

Joe Mallon
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.
September 1, 2014

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)
    }
}

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.
September 1, 2014

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.

Joe Mallon
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.
September 1, 2014

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)
    }
}

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.
September 2, 2014

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?

Joe Mallon
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.
September 2, 2014

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.

Joe Mallon
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.
September 4, 2014

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?

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.
September 4, 2014

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?

Joe Mallon
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.
September 16, 2014

That is correct.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2014

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

Joe Mallon
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.
August 29, 2014

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.

Suggest an answer

Log in or Sign up to answer