Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner Post Function Help (undeclared Variable)

Christopher Gronde
Contributor
October 19, 2016

 I am trying to create a script runner custom script post function that will allow our tickets Due Dates to be automatically set depending upon the Priority they are given. (e.g. a BLOCKER level ticket has 1 day to be due)  I have piecemealed together the below scripts from other similar scripts I've found here in the forum.  The script appears to be failing at "MutableIssue mutableIssue = (mutableIssue) issue;" telling me that the variable "issue" is undeclared.  I'm not sure what exactly I have to do in order to make this work.  Can anyone take a look and see if you can help me fix my script?

 

import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
 
// initiating the priority
 
def Blocker = 1;
def HIGH =2;
def MEDIUM = 3;
def LOW = 9;
def ROUTINE = 30;
 
// calendar which returns the date according to the priority defined
 
private GregorianCalendar getDate(double roll)
{
		GregorianCalendar cal = new GregorianCalendar();
		cal.setFirstDayofWeek(Calendar.MONDAY);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND,0);
 
		for (int x=0;x<roll;x++)
{
			cal.add(Calendar.DAY_OF_MONTH,1);
		}
 
		return cal;
}
 
MutableIssue mutableIssue = (MutableIssue) issue;
def priorityObject = mutableIssue.getPriorityObject();
def priority = priorityObject.getName();
def setDueDate = mustableIssue.getDueDate();
 
//only set the dueDate if the date isn't already set
 
		GregorianCalendar cal;
 
		if(priority.equals("Blocker"))
{
			cal = getDate(BLOCKER);
		}
	   else if(priority.equals("High"))
{
			cal = getDate(HIGH);
		}
	   else if(priority.equals("Medium"))
{
			cal = getDate(MEDIUM);
		}
	   else if(priority.equals("Low"))
{
			cal = getDate(LOW);
		}
	   else if(priority.equals("Routine"))
{
			cal = getDate(ROUTINE);
		}
 
		Timestamp dueDate = new Timestamp(cal.getTimeInMilliseconds());
 
		MutableIssue.getPriorityObject();
 
		MutalbeIssue.setDueDate(dueDate);

1 answer

1 accepted

1 vote
Answer accepted
Vasiliy Zverev
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.
October 19, 2016

Dear Kristofer, it seems that you do not have much experience in coding. Your script is too comples, since I simplified it:

import java.sql.Timestamp

// calendar which returns the date according to the priority defined
private GregorianCalendar getDate( int roll)
{
    GregorianCalendar cal = new GregorianCalendar();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.set(Calendar.HOUR_OF_DAY,0);
    cal.set(Calendar.MINUTE,0);
    cal.set(Calendar.SECOND,0);
    cal.set(Calendar.MILLISECOND,0);

    cal.add(Calendar.DAY_OF_MONTH,roll);

    return cal;
}


def setDueDate = mustableIssue.getDueDate();

//only set the dueDate if the date isn't already set

GregorianCalendar cal;

switch(issue.getPriorityObject().getName()){
    case "Blocker":     cal = getDate(1); break;
    case "High":        cal = getDate(2); break;
    case "Medium":      cal = getDate(3); break;
    case "Low":         cal = getDate(9); break;
    case "Routine":     cal = getDate(30); break;
}

issue.setDueDate(new Timestamp(cal.getTimeInMilliseconds()));

Also it is not clear about initial value for due date. Namely, does this script for issue creation step and due date = creation date?

Also I do not understand why do set due date to first day of a current week.

Vasiliy Zverev
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.
October 19, 2016

Note, that variable issue already defined into a postfunction with class MutableIssue.

Christopher Gronde
Contributor
October 20, 2016

You are correct I do not have much experience with Groovy script or coding.  I piecemealed this together from other similar scripts I found here in the forum.  This script will be a post function during the creation transition.  I'm assuming the initial due date should default to the date of creation and then add the amount of days required per Priority set.

Vasiliy Zverev
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.
October 20, 2016

Here is a updated version of code. I did not test it, but it should work:

import java.sql.Timestamp

GregorianCalendar calendar = new GregorianCalendar();

switch(issue.getPriorityObject().getName()){
    case "Blocker":     calendar.add(Calendar.DAY_OF_YEAR, 1); break;
    case "High":        calendar.add(Calendar.DAY_OF_YEAR, 2); break;
    case "Medium":      calendar.add(Calendar.DAY_OF_YEAR, 3); break;
    case "Low":         calendar.add(Calendar.DAY_OF_YEAR, 9); break;
    case "Routine":     calendar.add(Calendar.DAY_OF_YEAR,30); break;
}

issue.setDueDate( new Timestamp(calendar.getTimeInMillis()));
Vasiliy Zverev
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.
October 20, 2016

It also could be helpful for you: https://answers.atlassian.com/questions/32982259

Christopher Gronde
Contributor
October 20, 2016

now "def SetDueDate = mutableIssue.getDueDate();" is giving me a "variable 'mutable' is undeclared" error.  It also doesn't like the last line of your script.  It has three separate "cannot find matching method" errors.

Vasiliy Zverev
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.
October 20, 2016

Where do you execute this script. 

If into a postfunction, then variable issue have to be defined as MutableIssue.

If somewhere else you should get respective issue like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

import java.sql.Timestamp

//comment this line into postfunction
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")

GregorianCalendar calendar = new GregorianCalendar();

switch(issue.getPriorityObject().getName()){
    case "Blocker":     calendar.add(Calendar.DAY_OF_YEAR, 1); break;
    case "High":        calendar.add(Calendar.DAY_OF_YEAR, 2); break;
    case "Medium":      calendar.add(Calendar.DAY_OF_YEAR, 3); break;
    case "Low":         calendar.add(Calendar.DAY_OF_YEAR, 9); break;
    case "Routine":     calendar.add(Calendar.DAY_OF_YEAR,30); break;
}

issue.setDueDate( new Timestamp(calendar.getTimeInMillis()));
Christopher Gronde
Contributor
October 20, 2016

So I am running this a post function.  The script is running without errors but it doesn't seem to work.  Regardless of what I set the priority to if I search for the issue it shows todays date as the due date, and if I open the issue it doesn't even show me the due date on the details screen.

Vasiliy Zverev
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.
October 20, 2016

All postfunction on issue update must be after issue create postfunction. See postfunction list.

 

Christopher Gronde
Contributor
October 21, 2016

My post functions are as follows on the create transition:

  1. Creates the issue originally
  2. Re-index an issue to keep indexes in sync with database.
  3. Fire a Issue Created event that can be processed by the listeners.
  4. Script Runner post function script
Vasiliy Zverev
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.
October 21, 2016

I forgot that on create issue transition there is no store step. Here is updated version of code

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

import java.sql.Timestamp

GregorianCalendar calendar = new GregorianCalendar();

switch(issue.getPriorityObject().getName()){
    case "Blocker":     calendar.add(Calendar.DAY_OF_YEAR, 1); break;
    case "High":        calendar.add(Calendar.DAY_OF_YEAR, 2); break;
    case "Medium":      calendar.add(Calendar.DAY_OF_YEAR, 3); break;
    case "Low":         calendar.add(Calendar.DAY_OF_YEAR, 9); break;
    case "Routine":     calendar.add(Calendar.DAY_OF_YEAR,30); break;
}

issue.setDueDate( new Timestamp(calendar.getTimeInMillis()));
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false
)
Christopher Gronde
Contributor
October 21, 2016

So now the due date is finally showing up in the details of the ticket, but it's still not updating the date based on priority.  It's only marking it with today's date.

Christopher Gronde
Contributor
October 21, 2016

The calendar doesn't seem to be storing the additional days when you call the calendar.getTimeInMillis()....if I add + "some number" after that call I can manually add milliseconds, but it isn't calling the switch

Vasiliy Zverev
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.
October 21, 2016

I would recommend you to test script into script console.  

Make sure that priority names are correct.

Christopher Gronde
Contributor
October 24, 2016

Tested the script in the console and got the below error:

 

java.lang.NullPointerException: Cannot invoke method getPriorityObject() on null object

     at Script288.run(Script288.groovy:22)

Vasiliy Zverev
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.
October 24, 2016

Into script console you need to initilise issue variable, see comments:

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

import java.sql.Timestamp

//Comment it into a postfunction
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey")

GregorianCalendar calendar = new GregorianCalendar();

switch(issue.getPriorityObject().getName()){
    case "Blocker":     calendar.add(Calendar.DAY_OF_YEAR, 1); break;
    case "High":        calendar.add(Calendar.DAY_OF_YEAR, 2); break;
    case "Medium":      calendar.add(Calendar.DAY_OF_YEAR, 3); break;
    case "Low":         calendar.add(Calendar.DAY_OF_YEAR, 9); break;
    case "Routine":     calendar.add(Calendar.DAY_OF_YEAR,30); break;
}

issue.setDueDate( new Timestamp(calendar.getTimeInMillis()));
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false
)
Christopher Gronde
Contributor
October 25, 2016

yes I ran that version in the console...that's how I got that error.

Christopher Gronde
Contributor
October 25, 2016

I did also verify that the names of the priorities were correct including capitalization.

Vasiliy Zverev
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.
October 25, 2016

Did you changed "issueKey" to actual key of a tested issue?

Christopher Gronde
Contributor
October 25, 2016

When I change "issuekey" to ES-9634 the result at the bottom says "ES-9634" and the log tab says null

Christopher Gronde
Contributor
October 25, 2016

When I searched for the issue again NOW it shows the newly updated date...so it WORKS in the console!

 

Christopher Gronde
Contributor
October 25, 2016

So why doesn't it work as a post function?

Vasiliy Zverev
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.
October 25, 2016

Into post-function it is requred to comment line with variable "issue"

Christopher Gronde
Contributor
October 25, 2016

Should I run it as a Listener instead of a post function?

Vasiliy Zverev
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.
October 25, 2016

Listener is better if you have to do something on different workflow/transition.

So, if you need this only for this workflow then you should use postfunction, otherwise - make a listener.

Christopher Gronde
Contributor
October 25, 2016

I do comment out that line when I add it to the post function.  Still only updates the due date with today's date but then when I run it without that line commented in the console it will update the date to what I want it to be.

Vasiliy Zverev
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.
October 25, 2016

Could you provide list of post-functions?

Christopher Gronde
Contributor
October 25, 2016
  1. Create the issue originally
  2. Re-index an issue to keep indexes in sync with database
  3. Fire an Issue created event that can be processed by the listeners
  4. Script Runner post function script

(I have tried the script as step 3 and 4 with same result) 

Vasiliy Zverev
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.
October 25, 2016

Try to investigate atlassian-jira.log on JIRA server. Maybe some errors occur on transition.

Christopher Gronde
Contributor
October 26, 2016

There are no errors in the log after creating a new ticket.

Vasiliy Zverev
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.
October 26, 2016

Try place "Script Runner post function script" rigth after "Create the issue originally"

Christopher Gronde
Contributor
October 26, 2016

same outcome (Today's Date), no errors.

Vasiliy Zverev
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.
October 26, 2016

Lets try update description also:

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

import java.sql.Timestamp

//Comment it into a postfunction
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey")

GregorianCalendar calendar = new GregorianCalendar();

switch(issue.getPriorityObject().getName()){
    case "Blocker":     calendar.add(Calendar.DAY_OF_YEAR, 1); break;
    case "High":        calendar.add(Calendar.DAY_OF_YEAR, 2); break;
    case "Medium":      calendar.add(Calendar.DAY_OF_YEAR, 3); break;
    case "Low":         calendar.add(Calendar.DAY_OF_YEAR, 9); break;
    case "Routine":     calendar.add(Calendar.DAY_OF_YEAR,30); break;
}

issue.setDueDate( new Timestamp(calendar.getTimeInMillis()));
issue.setDescription(issue.getDescription() + " they killed Han Solo, bustards")
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , issue
        , EventDispatchOption.ISSUE_UPDATED
        , false
)
Christopher Gronde
Contributor
October 26, 2016

Ok so I don't know how or why, but it not only added the description you set, but it also set the correct due date.  I then commented out the setDescription and it still works with the due date...did you change anything else?

Christopher Gronde
Contributor
October 26, 2016

I also have the "MutableIssue issue =" commented out.

Vasiliy Zverev
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.
October 26, 2016

No, but it works, it is good.

Christopher Gronde
Contributor
November 1, 2016

This currently works in 6.4.4, am I going to have to change anything when we upgrade to 7.1.2

Vasiliy Zverev
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 1, 2016

Unfortunately we use JIRA 6.4since I can not test it for JIRA 7. Anyway, ask here for help if troubles appear.

Christopher Gronde
Contributor
November 3, 2016

Is there and if/then statement I can add to the script so that it does NOT update if someone manually sets the due date.  To only update if due date is blank?

Vasiliy Zverev
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 3, 2016

Here it is: if(issue.getDueDate = null){

}

Christopher Gronde
Contributor
November 4, 2016

thanks!  That kind of what I figured it would look like.  Thanks for all your help!

Faiza Kazmi May 8, 2019

how can i get issue key dynamically while putting on a transition in script runner post function

Faiza Kazmi May 8, 2019

@Vasiliy Zverev please find attached code its not picking up issue key when i create an issue and apply the code in post function

 

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.issue.field.CustomFieldUpdatedEvent;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue

//def projectComponentManager = ComponentAccessor.getProjectComponentManager()
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("");

 

 

def userUtil = ComponentAccessor.getUserUtil()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10604")
def cFieldValue = issue.getCustomFieldValue(cField).toString()

userUtil.createUserNoNotification(cFieldValue,"",cFieldValue,cFieldValue)

def groupManager = ComponentAccessor.getGroupManager()
def user = userUtil.getUserByName(cFieldValue)
def group = groupManager.getGroup("BM")

groupManager.addUserToGroup(user,group)

Vasiliy Zverev
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.
May 13, 2019

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")

This line returns issue with specified issue key. It is requared to test script into Script console.

Into postfunction there is a predefined variable named issue which represents current issue. You should comment this line into postfunction,

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events