Forums

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

Access Priority Field - Jira ScriptRunner Listener

Judah
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 23, 2019

I am trying to write a listener that will update the "priority" field of all sub-tasks when the parent is updated.

I currently have a listener that works on custom fields...However, since "priority" is not a custom field, my script returns a nullValue error when I try to access the priority field using the method getCustomFieldValue()

Further, the methods getFieldById() - getFieldByName() only work in behaviours. 

The ScriptRunner docs do not provide any valuable information for this specific case...

Regarding listeners:

WHAT METHOD CAN I USE TO ACCESS THE PRIORITY FIELD??

WHAT METHOD CAN I USE TO UPDATE THAT FIELD IN A SUB-TASK?

 

2 answers

1 accepted

1 vote
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 23, 2019

The Java API documentation for your version of Jira is your best friend.

When you look up the issue object, you will see that issue.getPriority() will return a Priority object 

def priority = issue.getPriority()
issue.subTaskObjects.each{subTask ->
subTask.setPriority(priority)
issueManager.updateIssue(currentUser, subTask, EventDispatchOption.DO_NOT_DISPATCH, false)
//don't forget to re-index the subtask
}
Judah
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 25, 2019

Thank you for the fast response! Here is my script thus far

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.lang.Object
import com.atlassian.jira.ofbiz.AbstractOfBizValueWrapper
import com.atlassian.jira.issue.IssueConstantImpl
import com.atlassian.jira.issue.priority.PriorityImpl
import com.atlassian.jira.issue.priority

Issue issue = event.issue

def priority = issue.getPriority()
def issueManager = ComponentAccessor.getIssueManager();

issue.subTaskObjects.each{subTask ->
subTask.setPriority(priority)
issueManager.updateIssue(currentUser, subTask, EventDispatchOption.DO_NOT_DISPATCH, false)
issueIndexManager.reIndex(subTask)
}

 

I get the error: unable to resolve class com.atlassian.jira.issue.priority (this import is taken from the docs you provided)

Without that import, the error is thrown on the method setPriority(), as it cannot recognize it without the import...

What am I missing? 

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 25, 2019

Here is a full script that works for me in my environment:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption

def issue= event.issue
def issueManager = ComponentAccessor.getIssueManager();
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def priority = issue.getPriority()
issue.subTaskObjects.each{subTask ->
subTask.setPriority(priority)
issueManager.updateIssue(currentUser, subTask, EventDispatchOption.DO_NOT_DISPATCH, false)
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
indexingService.reIndex(subTask)
ImportUtils.setIndexIssues(wasIndexing)
}

I do get static type checking errors. But they don't impact the ability to run.  

Like Judah likes this
Judah
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 25, 2019

Thank you again for the prompt response! 

You were correct about the static type checking errors. They led me to believe I was missing an import. 

My console shows two such errors but they do not impact that ability to run the script successfully.

I may throw a log in my script to note the errors. Other than that, everything works.

May riches and good fortune bless you for the remainder of your days! 

0 votes
siva
Contributor
August 10, 2021

@PD Sheehan 

How do i add the priority in the comment ?


i have defined 

def priority = issue.getPriority()

and in comment 

(priority)

i get an error 
---------------------------------------------------------------------
IssueConstantImpl[[GenericEntity:Priority][sequence,3]statusColor,#707070[name,3][iconurl,/images/icons/priorities/low.svg][description,Normal regression testing effort.][id,3]] 

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2021

If you have a priority object retrieved from issue.getPriority()  and you want to display it a new comment body string, you probably want to grab the name of the priority.

def priority = issue.getPriority()
def bodyString = "Some comment with prioirity name: ${priority.name}"

Suggest an answer

Log in or Sign up to answer