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?
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) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is correct.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, but you need to read the change history to find it - iterate over that looking for "your field" and oldvalue/newvalue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.