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


Do you have Cloud version or Server?

Hi Alexey, 

Thanks for the reply. I have a cloud version

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

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

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 ?

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

Hi @Alexey Matveev [cPrime] 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
def tdays = issue.fields[totaldays] as Integer

// Calculate the days
def days = edate-sdate

//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
(totaldays): days



thats the script i used 

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 [cPrime]

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


How should i handle this.

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked Dec 06, 2018 in Jira Service Desk

Looking for teams who switched from email to Jira Service Desk

The Jira Service Desk marketing team is working on a guide to help new Atlassian customers switch from email to JSD and we'd love to hear from you! Please share: - What made you realize that i...

1,194 views 10 13
View question

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