Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,535
Community Members
 
Community Events
165
Community Groups

Scriptrunner - cumulate costs of several issues and write into custom field of another issue

Stefan Salzl Community Leader Jun 02, 2022

Hi community,

I would need some hints/help with a use case I would like to implement with scriptrunner.

This is the situation:

I do have a board reflecting costs and budget for education. Within this board there are 2 different subtask issue types:

  • Subtask (Subtask)
  • course (Subtask)

 

The issue "Course" consists of a custom field "Total course costs". The issue type "Subtask" consists of a custom field "Budget".

I would like to populate the sum of the field "Total course costs" of all "Course" issues in status "Geplant" and write/populate this amount to the "Subtask" issue in status "Geplant". Please find the screenshots attached.

What would be your recommended way to implement this? 

Any help/hints appreciated.

 

Thanks in advance.

Best
Stefan

image.pngimage.pngimage.png

2 answers

1 accepted

1 vote
Answer accepted
Stefan Salzl Community Leader Jun 03, 2022

Could solve the problem with a lot of snippets and "try and error". Might not be the most professional script but as I am not a developer I am proud it does what it should:

Still any feedback/hints for improvement appreciated:

// gets the value of import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter

def cfManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.issueManager


// get all changes from event
log.warn(event)

// defines the issue where customfield will be selected
//def course = event.issue as MutableIssue
def isCourse = event.issue.issueType.name == "Course"
//log.warn("Course issue/trigger: " + course)
log.warn("isCourse: "+ isCourse)

//define custom field "Total course costs"
def cfCourseCosts = cfManager.getCustomFieldObject("customfield_xxxxx")

//define customfield "budget"
def cfBudget = cfManager.getCustomFieldObject("customfield_xxxxx")

//get issue to be updated (Budget Subtask)
MutableIssue budgetIssue = issueManager.getIssueByCurrentKey("KEY-xx")


// get all changes from event
def changeLog = event?.changeLog
log.warn("changeLog "+changeLog)

//check if either Field "Preis" or "Participants" changed
def changeItems = changeLog.getRelated("ChildChangeItem")
def field = changeItems.field[0]

def isPreisChange = field == "Preis"
def isParticipantsChange = field == "Participants"

log.warn("isPreisChange: "+isPreisChange)
log.warn("isParticipantsChange: "+isParticipantsChange)

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// The JQL query you want to search with
final jqlSearch = "project = XXX and issuetype=Course and status = Planning"

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}

if(isPreisChange || isParticipantsChange) {
try {
// Perform the query to get the issues
def cumulativeCourseCosts = (Double) 0
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
log.warn("issues: "+issues)
// log.info("result: " + issues)
issues.each {
// log.info(it.key)
def costs = (Float) it.getCustomFieldValue(cfCourseCosts)
log.warn(costs)
cumulativeCourseCosts = cumulativeCourseCosts + costs
log.info("current cumulative: " + cumulativeCourseCosts)

}
log.info("final calculation of course costs: " + cumulativeCourseCosts)
// issues*.key
//set value of budget field to newly calculated/current budget
budgetIssue.setCustomFieldValue(cfBudget, cumulativeCourseCosts)

//update issue
issueManager.updateIssue(user, budgetIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
//log.info(costArray)

} catch (SearchException e) {
e.printStackTrace()
null
}
}

Hi @Stefan Salzl ,

Not the complete or correct answer to your question, but in Automation for Jira it is also possible to add values ​​in a numerical field.

We had a situation at a customer where they use the following formula in Excel: ($D6+$E6+$F6+$G6)/2

This we recreated in Automation for Jira with
({{issue.Technical Standards}} + {{issue.Skills Capability & Training}} + {{issue.Scope & Complexity}} + {{issue.Risk Factors}}) /2
to include in 'edit issue'. See also the picture.

Maybe you may find this option useful.

Regards, Marco Brundel

image2019-5-1_14-18-25.png

Stefan Salzl Community Leader Jun 02, 2022

Hi @Marco Brundel ,

I would love to solve this with A4J. Unfortunately it‘s not an option as this is on Server and the plugin is not installed.

Thanks anyways for your effort.

Best
Stefan

Like Marco Brundel likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,113 views 34 44
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you