Forums

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

Scripted field to count number of fields with "pending" as the value?

Ashley Honeycutt
Contributor
April 15, 2025

Context:

There is a project with an issue type that requires buy-off from several teams. There is a field for each team that captures the status of their approval. Each field has the options of "pending" or "approved".

Desired state:

To make it easier to know how many approvals are left for each issue, we'd like a field that captures the number of these custom fields with "pending" set as the value. It needs to update automatically.

Example:

  • Team 1 Status = approved
  • Team 2 Status = pending
  • Team 3 Status = approved
  • Team 4 Status = pending
  • Remaining Approval Count = 2

1 answer

0 votes
Florian Bonniec
Community Champion
April 15, 2025

HI @Ashley Honeycutt 

You could use Automation and Scriptrunner for this. Create an automation based on edit event of the fields that are use for Approval status. Then when it happen, you use the Scriptrunner action.

The script could looks like:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

// Get the current issue
MutableIssue issue = issue

// Define the custom field names for the approval statuses
def approvalFields = ["Team 1 Status", "Team 2 Status", "Team 3 Status", "Team 4 Status"]

// Get the custom field manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// Initialize the count of pending approvals
int pendingCount = 0

// Iterate through the approval fields and count the ones with "pending" status
approvalFields.each { fieldName ->
CustomField customField = customFieldManager.getCustomFieldObjectByName(fieldName)
if (customField) {
String fieldValue = issue.getCustomFieldValue(customField) as String
if (fieldValue == "pending") {
pendingCount++
}
}
}

// Update the "Remaining Approval Count" custom field with the pending count
CustomField remainingApprovalCountField = customFieldManager.getCustomFieldObjectByName("Remaining Approval Count")
if (remainingApprovalCountField) {
issue.setCustomFieldValue(remainingApprovalCountField, pendingCount)
}

Another solution would be to use a Script fields.

Regards

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events