Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to manage checkboxes with Scriptrunner Listener

Anthony B October 5, 2020

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:

  • Sub-tasks have a 'Delivery Team' radio button picker
  • The Parent issues of these sub-tasks have a 'Delivery Team(s)' checkbox field.
    • This is designed to be a read-only field and reflects all of the selected Delivery Team values in each sub-task
  • I currently have a post-function in the sub-task creation step that will push the value of Delivery Team into the parent's Delivery Team(s) property.  This is done using the JSU plugin, so no code has been written.  This part works fine

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
}

 

1 answer

1 accepted

0 votes
Answer accepted
Jeroen Poismans
Community Champion
October 7, 2020

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 

Anthony B October 7, 2020

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

Jeroen Poismans
Community Champion
October 8, 2020

Hi Anthony,

Love it how you dove into the script and try to understand it!

  1. In my version of Jira and Scriptrunner i did not get this error, but I wish I had because this actually makes sense. Because you are making changes to the issue, it should be a MutableIssue. So, yes, this is correct.
  2. With "setCustomFieldValue" you place the changed customfield value on the issue object. With "updateIssue" you actually persist your changes. Without the "updateIssue" nothing happens on the actual issue.
  3. That's really up to the developer. I prefer to think that when I am updating a field on an Issue, I should use the IssueManager. But that is really personal preference. Both will work. There is one difference though, have a look at this line in my script:
    issueManager.updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

    With the IssueManager approach you can specify what Jira should do with it afterwards. The EventDispatchOption.DO_NOT_DISPATCH specifies that no update event is fired. The "false" param at the end specifies that no mails should be send for this update.

I hope this answers your questions!

Regards,

Jeroen

Anthony B October 8, 2020

Hi @Jeroen Poismans thanks very much for the clarifications, it is much clearer now :)

Suggest an answer

Log in or Sign up to answer