Hi,
I have a custom field (date type) that accepts users input to request for a due date for the project. Is there a way to set the Priority label once the ticket has been created to reflect what was chosen by the end user? Ideally i want to use High Priority if that date field was selected when the ticket was created.
Hi Uday,
you can use a groovy script for this, you can compare the date type customfield value with the currrent date and based on the difference you can set the priority.
you will need the script runner plugin for this and you will have to place your script as a post function on your create transition.
Below is a script which sets the duedate based on priority, hope you find it useful
import java.sql.Timestamp import com.atlassian.jira.issue.MutableIssue // initializing the priority def BLOCKER = 1; def CRITICAL = 2; def MAJOR = 3; def MINOR = 9; def Trivial = 30; // calender which returns the date according to the priority defined private GregorianCalendar getDate(double roll){ Calendar cal = Calendar.getInstance(); 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 priority = mutableIssue.getPriority().getString("name"); def setDueDate = mutableIssue.getDueDate(); //only set the dueDate if the date isn't already set (i.e. if it == null). GregorianCalendar cal; if(priority.equals("Critical")){ cal = getDate(CRITICAL); } else if(priority.equals("Major")){ cal = getDate(MAJOR); } else if(priority.equals("Minor")){ cal = getDate(MINOR); } Timestamp dueDate = new Timestamp(cal.getTimeInMillis()); mutableIssue.getPriorityObject(); mutableIssue.setDueDate(dueDate);
Hi Mizan,
Much appreciate you support in providing me with the code. Unfortunately it has been ages since i worked on java and much behind this. Let me see how i can use this .
Also i need the due date to be set after the ticket is created when the developer is seeing the ticket. So i am not sure which page needs to be edited to add your code.
Regards
Uday
Script runner plugin will make your task easy.
You just need to add a script post function on the create transition of ur workflow and give the path of the above groovy script.
Thansk i will try install Script Runner and see how far it goes. Thanks much for your help.
I set this script as a post function but it didn't work for me. The only change I made was renaming the priority values because we renamed them in our JIRA.
I saved the script to a txt file, maybe this can be the bug?
Thanks in advance! (setduedate.txt)
Best Regards,
Rumi
I was unable to make any progress on this and gave up. Wish this was built in as a standard feature by JIRA. I really wonder if we made a completely wrong decision to buy this product.
The extension has to be .groovy so the system knows which interpreter to invoke.
If you are willing to give up so soon without asking for any help, I really don't know what the right system is for you, but good luck in finding it.
Hi Jamie,
I changed the extension to .groovy manually, bit it still doesn't work. I summarize what I have done (maybe it can help for Uday too):
* I saved the script (setduedate.txt above) to a file
* I changed the extension to .groovy (is there a program that convert the extension or can it be changed manually?)
* I uploaded the script to a folder which our JIRA can reach
* I added a Script Post-Function to the Create Issue event
What can be a problem or missing something? The script-runner plugin is enabled in our JIRA but maybe the javascript is banned(?)
Or the script is not correct. Actually I never coded Javascript earlier but I can understand the script. I copied the script that Mizan had posted above and I changed only the priorities names because we use custom names (these are only captions, aren't they?). I guess all the JIRA works the same so the script might be the same for my JIRA too(?).
Thank you for all your trouble in help!
Best regards,
i assume you are using windows , open the script in a notepad , then file>>save as>>enter "filename"(setduedate.groovy)>>Enter "save as type"(all files) .
When you add the script to the postfunction provide the correct path+/setduedate.groovy
let me know what happens
when I renamed the file to 'setduedate.groovy' and chose Save as type (all files), it was saved as setduedate.txt again. Is it correct?
download this file . i have saved your script in .groovy format. I even doubt your script , the names of your priorities contain spaces not sure whether it is the correct way of naming variable . you can try using id's
Hi All, hi Mizan,
we just upgraded our Jira instance from 4.3.4 to Jira 5 and the script doesn't work anymore!
We also upgraded our Script Runner plugin to version 2.0.6
The script is the following:
log = Category.getInstance("com.onresolve.jira.groovy.PostFunction") log.debug("PostFunction function running") // initializing the priority def Sev1 = 1; def Sev2 = 2; def Sev3 = 3; def Sev4 = 9; // calender which returns the date according to the priority defined private GregorianCalendar getDate(double roll){ Calendar cal = Calendar.getInstance(); 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 priority = mutableIssue.getPriority().getString("name"); def setDueDate = mutableIssue.getDueDate(); //only set the dueDate if the date isn't already set (i.e. if it == null). GregorianCalendar cal; if(priority.equals("Sev 1 - Critical")){ cal = getDate(Sev1); } else if(priority.equals("Sev 2 - Urgent")){ cal = getDate(Sev2); } else if(priority.equals("Sev 3 - Normal")){ cal = getDate(Sev3); } else if(priority.equals("Sev 4 - Low")){ cal = getDate(Sev4); } Timestamp dueDate = new Timestamp(cal.getTimeInMillis()); mutableIssue.getPriorityObject(); mutableIssue.setDueDate(dueDate); log.debug("Due date set to"+dueDate) mutableIssue.store() log.debug("postfunction completed")
What could be changed because of the upgrade? I heard that it has to be set the groovy folder in the something Resource(??) but I don't where and what it is.
Can you please help?
Thanks in advance!
additional info:
the problem must related to the upgrade: I tested the script in a Jira version 4.3.4 and works great!
The Api's have changed . Can you provide the logs ? add some more log statement so that we can identify easily where the problem is .
The atlassian-jira.log file which is in the Jira home directory
The atlassian-jira.log which is in the Jira home directory .
How would i go about about setting the priority value? I need to hardcode the value of priority. The user will be allowed to chose a due date he is interestd in and if that field is not empty i would like to do a setPriority to the status called Priority. Below is the code we have in the groovy script. What i want to be able to do is replace
if (dueDate != null && highPriority != null) { -- such that if dueDate is null which means if that field is not blank then set the priority color to red. we only have 2 states which is normal and priority. I know that we need to udpate the below code
mutableIssue.setPriority(highPriority.getGenericValue());
To achieve that but i am not completely sure what i need to pass to setPriorityfunction to flag the value as red Priority. Is it a boolean?
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.config.ConstantsManager;
import com.atlassian.jira.issue.priority.Priority;
MutableIssue mutableIssue = (MutableIssue) issue;
def priority = mutableIssue.getPriorityObject();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
def dueDate = customFieldManager.getCustomFieldObjectByName("Priority Due Date");
def stt = customFieldManager.getCustomFieldObjectByName("Standard Turn Around Time");
if (priority == null || !priority.get("name").equals("Priority")) {
// pull the constant representing High Priority out of the database
ConstantsManager constantsManager = componentManager.getConstantsManager();
Priority highPriority;
for (Priority p: constantsManager.getPriorityObjects()) {
if (p.getName().equals("Priority")) {
highPriority = p;
}
if (dueDate != null && highPriority != null) {
log.info("GROOVY, due date is " + dueDate + " and its name is " + dueDate.getName()+ " and its value is " + dueDate.getValue(issue));
mutableIssue.store();
log.info("GROOVY, I set the priority to " + highPriority);
In the script you have mentioned I am facing an error in for loop section.
The error is 3 expressions are required for classic for loop you gave 4.
Please help me out here .
Thanks Karthik
It looks like you're new here. Sign in or register to get started.