Missed Team ’24? Catch up on announcements here.

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

Update Customfield listened using "ScriptRunner for JIRA"

Juan José Marchal Gómez
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.
November 25, 2019

Hello,

 

I created a script for update priority using Urgency and Impact. This script works as post function but when I'm using as listened is doing nothing. Any error appears in the code but the priority is not updated automatically.

Could you help me?

 

 

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


// This is a two dimensional map to represent the matrix
// Keys along left is urgency and second dimension keys is the impact
// This is a two dimensional map to represent the matrix
// Keys along left is urgency and second dimension keys is the impact
def priorityMatrix = [
"Very High - Totally stopped": ["All Plants": "Highest", "More than one plant": "Highest", "Partial or totally production plant / More than one department": "High", "Some users / One department / Individual user": "Medium"],
"High - Stopped": ["All Plants": "Highest", "More than one plant": "Highest", "Partial or totally production plant / More than one department": "High", "Some users / One department / Individual user": "Medium"],
"Medium - Works with major limitation": ["All Plants": "High", "More than one plant": "High", "Partial or totally production plant / More than one department": "Medium", "Some users / One department / Individual user": "Low"],
"Low - Works with limitation": ["All Plants": "Medium", "More than one plant": "Medium", "Partial or totally production plant / More than one department": "Low", "Some users / One department / Individual user": "Low"],
]

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def constantsManager = ComponentAccessor.getConstantsManager()
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def issue = event.issue as Issue
//def issueKey = "IESD-11905"
//def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueObject(issueKey)

def urgencyField = customFieldManager.getCustomFieldObjectsByName("Urgency")

def impactField = customFieldManager.getCustomFieldObjectsByName("Impact")

def urgency = issue.getCustomFieldValue(urgencyField[0]) as Option

def impact = issue.getCustomFieldValue(impactField[0]) as Option

def priorityName = priorityMatrix[urgency.value][impact.value]

def priority = constantsManager.getPriorities().find {it.name == priorityName.toString()}

def issueInputParameters = new IssueInputParametersImpl()

issueInputParameters.setPriorityId(priority.id)
issueInputParameters.setSkipScreenCheck(true)

issue.setPriorityId(priority.id)

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


if (validationResult.isValid()) {
issueService.update(user, validationResult)
} else {
log.warn validationResult.errorCollection.errors
}

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2019

Hi @Juan José Marchal Gómez ,

It should work in a listener if it works in a postfunction. Are you sure you mapped your script to the correct event so that it is triggered ? In that case try to add logs in your script to track any error and display the values of your variables.

Antoine

Juan José Marchal Gómez
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.
November 25, 2019

Hello @Antoine Berry 

thanks for the answer.

The issue has been runned 15 times since this configuration. The "triggered" of listener is issue updated.

Here you are the outcome.

of course No failures in the last 15 executions()

Custom listener
Projects: IESD
Events: Issue Updated
No note

LOGS:

Time (on server): Mon Nov 25 2019 13:36:24 GMT+0100 (Central European Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

No logs were found for this execution.

PAYLOAD:

{
    "projects": "[IESD] (java.util.ArrayList)",
    "issue": "IESD-13820 (com.atlassian.jira.issue.IssueImpl)",
    "log": "org.apache.log4j.Logger@10161a7d",
    "friendlyEventNames": "Issue Updated (java.lang.String)",
    "version": "3 (java.lang.Integer)",
    "name": "Custom listener (java.lang.String)",
    "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener (java.lang.String)",
    "id": "e608a472-ba24-4a06-93a1-2dbc956c28ea (java.lang.String)",
    "$beanContext": "com.onresolve.scriptrunner.beans.ScriptRunnerBackedBeanContext@5e714407",
    "event": "com.atlassian.jira.event.issue.IssueEvent@351ce617[issue=IESD-13820,comment=<null>,worklog=<null>,changelog=[GenericEntity:ChangeGroup][issue,32279][author,nacosta][created,2019-11-25 14:10:35.322][id,179504],eventTypeId=2,sendMail=false,params={eventsource=action, baseurl=https://helpdesk.emdep.com},subtasksUpdated=true,spanningOperation=Optional.empty]",
    "bundle": "com.atlassian.jira.event.issue.DefaultIssueEventBundle@35279e3a",
    "events": "[2] (java.util.ArrayList)"
}

 

Thanks in advance.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2019

What I meant is to add loggers lines in your script, such as :

issue.setPriorityId(priority.id)

log.error("issueInputParameters : " + issueInputParameters)
def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)
log.error("validationResult : " + validationResult)

if (validationResult.isValid()) {
issueService.update(user, validationResult)
log.error("issue updated")
} else {
log.warn validationResult.errorCollection.errors
}

to try and check what is wrong.

Juan José Marchal Gómez
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.
November 25, 2019

Hello @Antoine Berry 

Thanks for show me how to log information. I didn't know this option. I'm beginner in scriptRunner still.

The outcome is:

 

Time (on server): Mon Nov 25 2019 15:13:21 GMT+0100 (Central European Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-11-25 15:13:21,258 ERROR [runner.AbstractScriptRunner]: issueInputParameters : com.atlassian.jira.issue.IssueInputParametersImpl@2594a4a1
2019-11-25 15:13:21,258 ERROR [runner.AbstractScriptRunner]: validationResult : com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@4f5024ac
Juan José Marchal Gómez
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.
November 25, 2019

Working....

The only different is your recommendations of logging.

I don't understand but works...

Thanks a lot.

Like Antoine Berry likes this
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2019

Glad it worked out. I have been having a few strange cases where adding a log also solved the issue, and I still do not know why.

TAGS
AUG Leaders

Atlassian Community Events