How do I get the script to stop compounding the totalPoints from all issues?

David McEwan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 4, 2024
import java.lang.Double
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def highestOprStratField = customFieldManager.getCustomFieldObjectsByName("Annual Award")[0]
def jqlQueryParser = ComponentAccessor.getOSGiComponentInstanceOfType(JqlQueryParser)
def query = jqlQueryParser.parseQuery("issuetype = Personnel")
def results = searchService.search(user, query, com.atlassian.jira.web.bean.PagerFilter.getUnlimitedFilter())
def totalPoints = 0
def pointsMap = ["Yes": 5, "No": 0]
def pointsField = customFieldManager.getCustomFieldObjectsByName("Annual Award Points")[0]

results.getResults().each { issue ->
    def highestOprStratValue = issue.getCustomFieldValue(highestOprStratField)
   
    if (highestOprStratValue != null) {
        totalPoints += pointsMap.get(highestOprStratValue.toString(), 0)
    }

    // Update the issue with the calculated points
    def changeHolder = new DefaultIssueChangeHolder()
    pointsField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(pointsField), totalPoints as Double), changeHolder)
    def mutableIssue = issueManager.getIssueObject(issue.getKey())
    issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_UPDATED, false)
}

return totalPoints

0 answers

Suggest an answer

Log in or Sign up to answer