Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,362,956
Community Members
 
Community Events
168
Community Groups

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

0 votes
Answer 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 Sign up to answer
TAGS

Atlassian Community Events