Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Update priority field if user does not have a permission "Assign issues"

We are using ScriptRunner.
We are changing a priority field in Custom Listener when an event `Issue updated` is happened.
This code updates a `priority` field of issue if user has permission "Assign issues" in Issue Permissions tab of my project.
However, if user does not have permission "Assign issues" in Issue Permissions tab of my project, then this code is not updating priority field.
What can I do to update priority field if user does not have a permission "Assign issues", but user has a permission to "Edit Issues"?
In my view, it should be enough to have a permission "Edit Issues" to be eligible to edit priority field. Is it correct?

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl


def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue = event.issue
def idPriorityLoIssue = issue.getPriorityObject().getId().toString();

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sequenceField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Послед."}
log.info(sequenceField)
def sequenceFieldValue = issue.getCustomFieldValue(sequenceField)
log.info(sequenceFieldValue)


def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getOutwardLinks(issue.id).each{ issueLink ->
def linkedIssue = issueLink.getDestinationObject()
log.warn('1. ***** LinkedIssue: *****' + linkedIssue)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.warn('1.0. User is' + user)
def issueInputParameters = new IssueInputParametersImpl()
def issueService = ComponentAccessor.getIssueService()
issueInputParameters.setPriorityId(idPriorityLoIssue.toString())
issueInputParameters.setSkipScreenCheck(true)
def validationResult = issueService.validateUpdate(user, linkedIssue.id, issueInputParameters)
log.warn('validationResult.isValid() ' + validationResult.isValid())

if (validationResult.isValid()) {
log.warn('vaild')
issueService.update(user, validationResult)
log.warn('2. ***** Priority of issue is set to: *****' + idPriorityLoIssue)
def seqFieldLinkedIssue = customFieldManager.getCustomFieldObjects(linkedIssue).find {it.name == "Послед."}
if(seqFieldLinkedIssue){
def changeHolder = new DefaultIssueChangeHolder()
seqFieldLinkedIssue.updateValue(null, linkedIssue, new ModifiedValue(linkedIssue.getCustomFieldValue(seqFieldLinkedIssue), sequenceFieldValue),changeHolder)
}
} else {
log.info('not vaild')
log.warn validationResult.errorCollection.errors
log.info('3. ***** Priority of issue is NOT set to: *****' + idPriorityLoIssue)
}
}

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin Community Leader Dec 24, 2018

I suggest using IssueManager to update issues instead of IssueService. 

Like so:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

issue.setPriority(priority)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

@Ivan Tovbin Thanks for your answer. I've tried to use your code, but the following code is outlined by red color:

 

issue.setPriority(priority)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

The error says:

1. Cannot find matching method "setPriority"

2. Cannot find matching method "updateIssue"

Ivan Tovbin Community Leader Dec 28, 2018

Can you show me the code as it looks like on your end?

So the final code should look like this:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category
import java.sql.Timestamp
import com.atlassian.jira.event.type.EventDispatchOption;

def log = Logger.getLogger("com.acme.CreateSubtask")
IssueManager issueManager = ComponentAccessor.getIssueManager()

def issue = issueManager.getIssueObject("Bar-1")

def customFieldManager = ComponentAccessor.getCustomFieldManager()


def environmentValue = issue.getEnvironment();
log.info('environmentValue ' + environmentValue)
issue.setEnvironment('New Environment 1')


def user = ComponentAccessor.getJiraAuthenticationContext()
.getLoggedInUser()
log.info('user ' + user)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH
, false)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

67 views 0 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you