Hello, I have been struggling to get a Listener script working. I am new to scripting. Hopefully someone can shed some light on what I need to do :)
I want to keep a custom field (checkbox) up-to-date whenever anything changes in any of the subtasks. My current set up is:
I want to make the process more robust and listen out for when any of the sub-tasks are updated and/or deleted, using a Scriptrunner Listener.
When the event fires on the sub-task I want to get the 'Delivery Team(s)' custom field from the parent issue and wipe it clean. I then want to loop through each of the sub-tasks, get the 'Delivery Team' custom field and add it to the 'Delivery Team(s)' custom field in the parent.
This way, whenever something changes in the sub-tasks I rebuild the 'Delivery Teams(s)' property in the parent issue
I am struggling to understand how to get the checkbox, wipe it clean and re-select options. Any help is greatly appreciated!
For info, this is the code I have so far, but is a long way off from doing anything!
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueFieldConstants.*
// Custom field identifiers
final String TEAM = "customfield_1234";
final String DELIVERY_TEAMS = "customfield_5678";
// Apply logic only to the Deliverable issue type
if (issue.getIssueType().name == "Deliverable") {
def parentIssue = issue.parentObject
def childIssues = parentIssue.getSubTaskObjects()
def cfTeam = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(TEAM)
def cfDeliveryTeams = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(DELIVERY_TEAMS)
// If the custom fields do not exist, exit
if (!cfTeam || !cfDeliveryTeams) {
return
}
//Clear the Delivery Team field in the parent Object
def cfDeliveryTeamsValue = parentIssue.getCustomFieldValue(cfDeliveryTeams)
// TODO: totally stuck here
// Cycle through each subtask to get the Delivery Team property value
childIssues.each {
//Add the Delivery Team property value to a List
// TODO
}
// Update Delivery Team(s) custom field with values from the sub-tasks
// TODO
} else {
// The event was not on a Deliverable ticket, so exit
return
}
Hi Anthony,
This code snippet should get you pretty far, does exactly that.
This is in the assumption that the radio button options are labeled the same as the checkbox options (for example Team1, Team2, Team3 ... ).
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// This is your parent issue
Issue parentIssue = issueManager.getIssueObject("TEST-1")
// Get The customfields
CustomField checkBoxCf = customFieldManager.getCustomFieldObject(10100L) // ID of your readonly field on the parent
CustomField radioCf = customFieldManager.getCustomFieldObject(10101L) // ID of your Radiobutton field of the subtasks
List<Issue> subtasks = parentIssue.getSubTaskObjects()
List teamsToAdd = new ArrayList()
// Loop over subtasks and add the selected radio value to a list
for (Issue subtask : subtasks) {
def radioValue = subtask.getCustomFieldValue(radioCf)
if (radioValue != null && !teamsToAdd.contains(radioValue)) {
teamsToAdd.add(radioValue)
}
}
// Set the Checkboxes and store the issue without Update event
parentIssue.setCustomFieldValue(checkBoxCf, teamsToAdd)
issueManager.updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
Let me know if this worked!
Jeroen
Hi @Jeroen Poismans thanks so much for your help - I have managed to get it all working now thanks to your guidance / code :D
I do have a couple of questions just to try and understanding things better;
1 - I was getting an error with this line;
parentIssue.setCustomFieldValue(checkBoxCf, teamsToAdd)
To solve it I had to make the parentIssue object a MutableIssue like this
MutableIssue parentIssue = (MutableIssue)issue.parentObject
Do you see any problem with this?
2 - Can I ask what the difference is between "setCustomFieldValue" and "updateIssue"?
3 - Related to the second question, on another script I created (using code sourced from these forums) I update a custom field using this method
def changeHolder = new DefaultIssueChangeHolder()
myCustField.updateValue(null, issue, new ModifiedValue(cFieldECValue, COLOUR_GREEN),changeHolder)
Is there much difference between this approach and the one in your solution? Is one favoured over the other?
Many thanks again
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anthony,
Love it how you dove into the script and try to understand it!
issueManager.updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
I hope this answers your questions!
Regards,
Jeroen
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.