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:
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.