The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.