Build Calculated customfield values based on JQL query customfields

_Uday Kiran February 20, 2024

Hi Team,

I have 2 different custom fields (Field A, Field B) in Jira datacenter. I was looking to build a new calculated custom field (Field C) where the values will be the sum-up of Field B on a specific JQL search.

 

Example: I have a JQL query "Field A = xx" this will return few issues. Now based on the results, I would like to sumup the values of 'Field B' and populate them on 'Field C'

same way I would like to add multiple conditions

I have a JQL query "Field A = yy" this will return few issues. Now based on the results, I would like to sumup the values of 'Field B' and populate them on 'Field C'

 

Is this possible to achieve?

2 answers

0 votes
Ram Kumar Aravindakshan _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.
February 21, 2024

Hi @_Uday Kiran

For your requirement, a Scripted Field is a straight forward approach.

Below is a sample code for your reference:-

import com.adaptavist.hapi.jira.issues.Issues

def jql = "'Number 1' > 10"

Issues.search(jql).collect {
it.getCustomFieldValue('Number 2') as Long
}.sum()

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Scripted Field configuration:-

scripted_field_config.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

_Uday Kiran February 21, 2024

Hi Ram, 

Thanks for your response.

_Uday Kiran February 21, 2024

Also, Can we include an another logic here?

Suppose user selects  customfield A (Single-Select) value as 'A' on create or edit screen

Script should automatically make a JQL search as customfield_A = A and from the results calculate the values in customfield B (Number field) and display the sum in Customfield C (Scripted Field)

 

Same way should happen on everytime the user selects a value from customfield A.

 

Below script doesnt really work:

import com.atlassian.jira.component.ComponentAccessor

// Replace 'Customfield_A', 'Customfield_B', and 'Customfield_C' with actual field names or IDs
def customfieldAName = 'Customfield_A'
def customfieldBName = 'Customfield_B'
def customfieldCName = 'Customfield_C'

def issue = issue

def customfieldAValue = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customfieldAName))

if (customfieldAValue) {
def jql = "'${customfieldAName}' = '${customfieldAValue}'"

def totalSum = 0
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

def query = jqlQueryParser.parseQuery(jql)
def searchResults = searchService.search(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), query, PagerFilter.getUnlimitedFilter())

searchResults.getResults().each { searchIssue ->
def customfieldBValue = searchIssue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customfieldBName)) as Long
totalSum += customfieldBValue ?: 0
}

issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customfieldCName), totalSum)
}

0 votes
Kalyan Sattaluri
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.
February 20, 2024

Hello @_Uday Kiran 

Yes, its possible with automation. You can have a scheduled rule which does this for you on daily basis and populate field C which I am guessing already exists.. You havent given any specifics so hard to go any further. Start with the rule and share some screenshots with actual fields and we can help.

Suggest an answer

Log in or Sign up to answer