How to update the RAG Status via ScriptRunner
Overview
This section shows the instructions to use the ScriptRunner to update the RAG Status field automatically when the issue is overdue.
The Rag Status field doesn't show on the Automation for Jira.
Demonstration
Paste the following code into the Inline script field. This script will
execute JQL query to find a list of overdue issues and
set the RAG Status field of each matching issue to Red.
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
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.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)
def userManager = ComponentAccessor.getUserManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.customFieldManager
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService.class)
// edit this query to suit
def query = jqlQueryParser.parseQuery("project = LOOKUP and dueDate < now()");
def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
log.debug("Total issues: ${results.total}")
results.getResults().each {result ->
def issue = issueManager.getIssueObject(result.id)
//Set the name of the custom field to update
final customFieldName = 'Result Status'
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField : "Could not find custom field with name $customFieldName"
def existingValue = (String) issue.getCustomFieldValue(customField)
//Set the new value of the field
def newValue = 'Red'
def fieldConfig = customField.getRelevantConfig(issue)
def option = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find {
it.toString() == newValue
}
issue.setCustomFieldValue(customField, option)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.debug("Updated ${issue.key} from ${existingValue} to ${newValue}")
}