script runner basic help

 would like to write a script which calclautes the differnce between two date fields and display the result as number of days in a custom text field.

Field 1 : start date

Field 2 : end date

Field 3 . total number of days.

Expected result:

Filed 3 = field 2 - field 1

after a lot of search and suggestions i chose the script runner plugin. But i dont understand how this script runner plugin works and where should i start writing my script.

Insights will be much helpful for a newbie

 

1 answer

1 vote
Alexey Matveev Community Champion Nov 28, 2017

Hello,

Do you have Cloud version or Server?

Hi Alexey, 

Thanks for the reply. I have a cloud version

Alexey Matveev Community Champion Nov 28, 2017

You need to create 3 custom fields. Then you have to add a scriptrunner post function for the transition where the third field must be calculated. The code for the post function would be like here

http://scriptrunner-docs.connect.adaptavist.com/jiracloud/post-functions.html#_calculated_custom_field

by custom field you mean the available date picker fileds rights ? or should i create my own custom fields..

Alexey Matveev Community Champion Nov 28, 2017

yes, available date picker fields

cool, where should i add my script. should i write my scriot on the script listener or some where else ?

Alexey Matveev Community Champion Nov 28, 2017

into post function. open desired transition and add scripted post function

Hi @Alexey Matveev i tried like what you said. but i am not able to see the expected reasult..

// custom field references

// date picker 1 
def startdate = 'customfield_10401'  

// date picker 2

def enddate = 'customfield_10403'

// text field where i want my result of the date difference calculation

def totaldays = 'customfield_10406'

// Extract the existing values from the issue
def sdate = issue.fields[startdate] as Integer
def edate = issue.fields[enddate] as Integer
if (sdate == null && edate == null) {
// No date's was specified, we can't calculate the date
return
}
def tdays = issue.fields[totaldays] as Integer

// Calculate the days
def days = edate-sdate

put("/rest/api/2/issue/${issue.key}")
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
.body([
fields:[
(totaldays): days
]
])
.asString()

 

 

thats the script i used 

Alexey Matveev Community Champion Nov 28, 2017

I think you have problems with converting dates. You need to use date arithmetic. The code which substract dates would look like this

def duration = groovy.time.TimeCategory.minus(
  new Date(),
  new Date(session.creationTime)
);
def values = [
            "seconds: " + duration.seconds,
            "min: " + duration.minutes,
            "hours: " + duration.hours,
            "days: " + duration.days,
            "ago: " + duration.ago,
];

You totaldays custom field must be numeric not date picker. 

Unfortunately, I can not check your code because I do not have Jira Cloud. I have only Jira server and scripts are different there since rest api is used in Cloud and in server - jira api.

Hi @Alexey Matveev

I am getting an error "the variable [issue] is undeclared.

 

How should i handle this.

Suggest an answer

Log in or Join to answer
Community showcase
Emilee Spencer
Published yesterday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

50 views 0 3
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot