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);
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It also could be helpful for you: https://answers.atlassian.com/questions/32982259
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
All postfunction on issue update must be after issue create postfunction. See postfunction list.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My post functions are as follows on the create transition:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I would recommend you to test script into script console.
Make sure that priority names are correct.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes I ran that version in the console...that's how I got that error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did also verify that the names of the priorities were correct including capitalization.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you changed "issueKey" to actual key of a tested issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When I change "issuekey" to ES-9634 the result at the bottom says "ES-9634" and the log tab says null
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When I searched for the issue again NOW it shows the newly updated date...so it WORKS in the console!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Into post-function it is requred to comment line with variable "issue"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Should I run it as a Listener instead of a post function?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you provide list of post-functions?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
(I have tried the script as step 3 and 4 with same result)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try to investigate atlassian-jira.log on JIRA server. Maybe some errors occur on transition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There are no errors in the log after creating a new ticket.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try place "Script Runner post function script" rigth after "Create the issue originally"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I also have the "MutableIssue issue =" commented out.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No, but it works, it is good.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This currently works in 6.4.4, am I going to have to change anything when we upgrade to 7.1.2
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Unfortunately we use JIRA 6.4since I can not test it for JIRA 7. Anyway, ask here for help if troubles appear.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here it is: if(issue.getDueDate = null){
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks! That kind of what I figured it would look like. Thanks for all your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
how can i get issue key dynamically while putting on a transition in script runner post function
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.