Script Runner Post Function Help (undeclared Variable)

 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

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.

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

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.

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()));

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.

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()));

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.

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

 

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

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
)

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.

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

I would recommend you to test script into script console.  

Make sure that priority names are correct.

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)

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
)

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

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

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

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

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

 

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

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

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

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.

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.

Could you provide list of post-functions?

  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) 

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

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

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

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

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
)

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?

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

No, but it works, it is good.

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

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

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?

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

}

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

358 views 0 6
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