imports / getting started with groovy script runner

Hi all,

 

i am about to learn scripting due to my subsidiary tasks as a jira admin. What I'm trying atm ist to calculate the days between two datefield values.

 

What kind of imports do I need before starting? I've seen some imports in other questions based on scriptrunner e.g.:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;

 

what are these imports doing and is there a list of imports I need based on what I want to script?

 

br,

shippo

2 answers

1 accepted

This widget could not be displayed.

Hi, you need to import every class which is going to be used in your script except classes from java.lang package. It looks like you're not Java or Groovy programmer, right? So here is some information about import keyword in JAVA/Groovy:

https://en.wikibooks.org/wiki/Java_Programming/Keywords/import

For your task you will need to import at least:

  • com.atlassian.jira.issue.fields.CustomField
  • com.atlassian.jira.issue.CustomFieldManager
  • java.util.Date

You will probably need some more imports but you will find out from error which will be thrown by your script.

Feel free to write more details here...

You're right i am neither a groovy nor a java programmer.

 

In the first step i want to calculate the days between two dates and return the value as days in a read-only text field. I've two custom field (date picker) called Start Date and End Date. After a user chooses these two dates i want my script field to display the days between these two dates. do you have an example how to picture this? In the next step i want to generate some more complex conditions e.g. showing a field based on the selection in a drop down field, and picture a field called complexitiy wich displays different complexitiy sizes from XS to XL based multiple field values. I would know how to start with java in eclipse hehe but i've difficulties with scripting in jira with groovy as mentioned

Sorry, I don't have any script like that and I think that this community forum is much more about helping, not about solving issues which are often paid ;). So let's try to do it with "helping".

First you will have to prepare new scripted field:

https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_tips_tricks_and_gotchas

In your script you have to get insances of your both Date custom fieds. You can use ComponentAccessor#getCustomFieldManager() - https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/component/ComponentAccessor.html#getCustomFieldManager--

Then you can get values of these fields using:

https://docs.atlassian.com/software/jira/docs/api/7.0.6/com/atlassian/jira/issue/fields/CustomField.html#getValue-com.atlassian.jira.issue.Issue-

You will get java.util.Date values from both fields. You can use following solution to get days between two days:

https://stackoverflow.com/a/30184795

Good luck and feel free to write again if you're stucked but after some research first, pls :).

Martin

Thanks a lot, Appreciate your help :)

This widget could not be displayed.
Ivan Tovbin Community Champion Jan 12, 2018

Hi Celik,

If you just need the difference in days, then the script is rather simple. Create a scripted field which will be used in your issues to display your days difference. Assuming you know how to look up your custom fields' IDs use the below script for your scripted field:

import com.atlassian.jira.component.ComponentAccessor

def cfMgr = ComponentAccessor.getCustomFieldManager()
def startDate = cfMgr.getCustomFieldObject(11111.toLong())
def endDate = cfMgr.getCustomFieldObject(22222.toLong())

if (issue.getCustomFieldValue(startDate) != null && issue.getCustomFieldValue(endDate) != null){
return issue.getCustomFieldValue(endDate) - issue.getCustomFieldValue(startDate)
}
return null

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

79 views 1 0
Join discussion

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