Scriptrunner custom listner update status Edited

Dears,

 

I am trying to update the priority of an issue based on impact and urgency. I have all bits of code, but the last step (updating the priority is not working. It also does not trigger an error.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.priority.Priority
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption;

log.error(event.issue.priority)
log.error(event.issue.priority?.name)


MutableIssue currentIssue = event.issue as MutableIssue
Issue issue = event.getIssue();
def impact = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Impact")
log.error(event.getIssue().getCustomFieldValue(impact))
def urgency = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Urgency")
log.error(event.getIssue().getCustomFieldValue(urgency))
String priority="Low";
log.error(event.issue.priority)
for(Priority priority1: ComponentAccessor.getConstantsManager().getPriorities()){
    log.error(priority1.getName() + " " +priority )
    if(priority1.getName().equals(priority)){
        log.error("Woehoe" )
        currentIssue.setPriorityObject(priority1)
        currentIssue.setPriorityId(priority1?.id)
        
    }
}
log.error(event.issue.priority)
ComponentAccessor.getIssueManager().updateIssue(null, currentIssue, EventDispatchOption.ISSUE_UPDATED, false);

log.error(event.issue.priority)



in meantime I have done additional tests. Apparently if my script ran, the issue priority is set. But in the background something is setting back the priority to it's previous sate.

 

 

 

 

 

 

 

 

 

1 answer

1 accepted

The issue is solved and I guess it is related to the following fact. The trigger is executed prior to the actual update. The changes I am doing in the code above is on the issue and not on the updateIssue. Therefore it get's overridden. Following code does the trick:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.IssueInputParametersImpl

import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.issue.priority.Priority

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.event.type.EventDispatchOption;

 

//set default values

String impactDefault = "Medium"

String UrgencyDefault = "Medium"

 

//create variables

def issue = event.issue as Issue

def impact = impactDefault

def urgency = UrgencyDefault

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

//get helpers

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def constantsManager = ComponentAccessor.getConstantsManager()

def issueService = ComponentAccessor.getIssueService()

 

//get the custom fields for the priority check

try{

    def urgencyField = customFieldManager.getCustomFieldObjectByName("Urgency")

    urgency = issue.getCustomFieldValue(urgencyField) as String

    if(urgency==null){urgency=UrgencyDefault}

}catch(Exception ex){}

try{

    def impactField = customFieldManager.getCustomFieldObjectByName("Impact")

    impact = issue.getCustomFieldValue(impactField) as String

    if(impact==null){impact = impactDefault}

}catch(Exception ex){}

 

//define the priority

String priority ="";

 

log.error("U:" + urgency + " I:" + impact)

switch(urgency.trim()){

    case "High":

       log.error("Switch: " + urgency)

       switch(impact.trim()){

            case "High":

            log.error("Switch: " + impact)

            priority = "Highest"

            break;

            case "Medium":

            log.error("Switch: " + impact)

            priority = "High"

            break;

            case "Low":

                    log.error("Switch: " + impact)

            priority = "Medium"

            break;

       }

    break;

    case "Medium":

             switch(impact.trim()){

            case "High":

                priority = "High"

            break;

            case "Medium":

                priority = "Medium"

            break;

            case "Low":

                priority = "Low"

            break;

       }

    break;

    case "Low":

       switch(impact.trim()){

            case "High":

            priority = "Medium"

            break;

            case "Medium":

            priority = "Low"

            break;

            case "Low":

            priority = "Lowest"

            break;

       }

    break;

}

log.debug(priority)

 

//create an object for the input parameters

def issueInputParameters = new IssueInputParametersImpl()

 

//get the priorities

for(Priority priority1: ComponentAccessor.getConstantsManager().getPriorities()){

    log.error(priority1.getName() + " " +priority )

    if(priority1.getName().equals(priority)){

        issueInputParameters.setPriorityId(priority1?.id)       

    }

}

 

issueInputParameters.setSkipScreenCheck(true)

 

//validate the update

def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

 

 

//execute the updat

if (validationResult.isValid()) {

    issueService.update(user, validationResult)

} else {

    log.error validationResult.errorCollection.errors

}

Kind regards,

Matt

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

409 views 1 13
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot