Forums

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

Set parent issue custom field value depending on sub-tasks's values

Deleted user September 24, 2018

Hi

I'm currently trying to set a parent issue's custom field (single choice select list) based on the values for the same custom field in its sub-tasks.

It's a simple select list with as answers 'Yes', 'No', and 'Unspecified'. The field is used to see if there is an impact for capacity planning. This means that if one of the sub-tasks has 'Yes' as a value, the parent issue should automatically be 'Yes' as well, because there is an impact.

If there are no 'Yes' answers found in the sub-tasks, it may still be unspecified, so the parent issue shouldn't say no just yet. If there are no 'Unspecified' values found either, then we can conclude that 'No' should be set for the impact.

I had created the following script and added it as an initialiser in ScriptRunner Behaviours and mapped it to the issue type and correct project. However, with the following code, Jira seems to get stuck in loading and crashes (or at least is unaccessible for a couple of minutes). I'm pretty new to groovy scripting in Jira, so I'm probably using wrong things mixed together.

Thanks in advance!

------------------------------CODE-----------------------------------------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager

def cfName = "Performance Reporting impact"
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName(cfName)

boolean val = false
boolean unspecified = false

def subtasks = underlyingIssue.getSubTaskObjects()

if(subtasks == null)
 return

if(customField == null)
 return

for(subtask in subtasks)
{
    if(subtask.getCustomFieldValue(customField) == 'Yes')
    {
        val = true
    }
   
    if(subtask.getCustomFieldValue(customField).toString() == 'Unspecified')
    {
        unspecified = true
    }
   
}

if(val)
{
    def fieldConfig = customField.getRelevantConfig(underlyingIssue)
    def param = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Yes' }
    getFieldByName(cfName).setFormValue(param)

}
else
{
    if(unspecified)
    {
        def fieldConfig = customField.getRelevantConfig(underlyingIssue)
      def param = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Unspecified' }
        getFieldByName(cfName).setFormValue(param)

    }
    else
    {
        def fieldConfig = customField.getRelevantConfig(underlyingIssue)
      def param = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'No' }
        getFieldByName(cfName).setFormValue(param)
    }  
}

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Ivan Tovbin
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 28, 2018

Hi Jorden,

I might be able to help you here. Please advise when exactly do you want this to trigger? 

Deleted user September 28, 2018

I wanted to have this calculated when a users was viewing the issue, so the value would always be up to date.

Also currently I found that this part of the code:

def fieldConfig = customField.getRelevantConfig(underlyingIssue)
      def param = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'OPTION NAME' }
        getFieldByName(cfName).setFormValue(param)

throws a stack overflow in the error log of jira.

 

But today I think I read that it would be better to create the functionality with Scripted Fields? 

If I were to use scripted fields, would it then be saved in the issue as well? (so it shows up when you do a JQL search for it) And how would I go about that?

 

Thanks!

Kind regards

Jorden

Ivan Tovbin
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 29, 2018

Indeed, scripted field seems to be the best solution here. As far as JQL search is concerned it should be fine as long as you pick the correct searcher in your field config.

As for the code, it is as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
CustomField cf = cfMgr.getCustomFieldObjectByName("Performance Reporting impact")
Collection<Issue> subtasks = issue.getSubTaskObjects()
String result = "No"
if(subtasks){
if(subtasks.any{it.getCustomFieldValue(cf).getValue().equals("Yes")}){
result = "Yes"
}else if(subtasks.any{it.getCustomFieldValue(cf).getValue().equals("Unspecified")}){
result = "Unspecified"
}
}
return result
Deleted user September 30, 2018

Hi Ivan

Thank you for your response. In your code I see the result is given as a string value. Is it also possible to have this applied to a select list?

The custom field that I'm working with is on both parent and sub-tasks and should be a simple "Yes" or "No" select to prevent people from typing mistakes in the sub-tasks if I were to use text fields. Your current solution would work if I either made the field a text field or if I had a text field for the parent issue and a select list for the sub-task issues.

Is there a way to make it work with select lists as well? Or am I completely of track and should this work for this case as well?

Thanks!
Kind regards
Jorden

Muddassir Quazi
Contributor
January 2, 2023

Hi Ivan,

I am trying the same thing on transition. Where the parent has the same field on transition. When any subtask has an option selected as Failed/Roll Back then the same options should be present for the parent also. The field is the same for parent and child.

Means, the other options available are Successful, Partial Success, these should not be present for parent if subtask has Failed/Roll Back selected.

How this can be implemented in such a case?

Can you please help us with this?

Regards,

Muddassir Quazi

TAGS
AUG Leaders

Atlassian Community Events