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

[static type checking] The variable [issue] is undeclared

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 21, 2019

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

// get current issue
def issue = issue as Issue

 

Hello, this is my first scriptRunner script and...this is embarrassing but...I don't find why the variable is undeclared..

 

best regards and thanks in advance.

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Leonard Chew
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 21, 2019

Get a specific issue on Script Console:

 

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

import com.atlassian.jira.component.ComponentAccessor

def issueKey = "TEC-123"
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject(issueKey)

log.debug('my issue summary is: ' +issue.summary)
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 22, 2019

Hello Leonard,

thanks a lot. Works.

But, I'm trying to create a variable matching 2 custom fields of selected issue.

So, what I need is that the "TEC-123" instead of a specific issue, will be a "parameter".

For example, If I'm running this script in a transition, I want that be the issue of the transition.

How I should declare it in my script to give the "issue transitioned"?

Best regards.

Leonard Chew
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 22, 2019

If you are running a postfunction script in a transition, then there is no need to get the "issue" object, as it is already per default set to the issue that is being transitioned.

Create a postfunction script with this code:

log.warn('my transitioned issue is: ' +issue.summary)

When the script gets executed (make sure you transition an issue based on this workflow), you will see that it works without anything else. 

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 22, 2019

Thanks again Leonard,

what I'm starting to understand is, depend on the place that you are "doing" the script, needs or not some declaration. I will keep care with this.

Can I tell you one last question? only for understanding that I'm in the right way for working with ScriptRunner.

I created the post function and added to the transition. I checked but nothing happens.

The outcomes of this scripts, where I can see?

Because after running, where is saving the information, If I want to see in the screen I should save in a new script custom field?

Best regards.

Leonard Chew
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 22, 2019

A summary of the last 15 executions can be seen if you expand your postfunction.

Everything you log can be seen in the log section.

image.png

See also:

https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-post-functions-tutorial.html

0 votes
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

Here you are the first script.

This script is runned as postfunction. For begginers, some comments:

 

1) Check it first in console. For doing it you can use the following lines that will be commented when you will save as postfunciton.

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

2) You can use "return Variable" to check if the script is going well step by step. For example, after the line  

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

 

you can do return priorityName.name and if the information is not null, this is correct.

Best regards.

Thanks in advance to @Leonard Chew

Within him, this first automation wasn't done.

 

__________________________________________________________________________

 

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 = 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)

TAGS
AUG Leaders

Atlassian Community Events