Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

script runner basic help

Karthik Mohan November 28, 2017

 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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2017

Hello,

Do you have Cloud version or Server?

Karthik Mohan November 28, 2017

Hi Alexey, 

Thanks for the reply. I have a cloud version

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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

Karthik Mohan November 28, 2017

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

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2017

yes, available date picker fields

Karthik Mohan November 28, 2017

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

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2017

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

Karthik Mohan November 28, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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.

Karthik Mohan November 28, 2017

Hi @Alexey Matveev

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
TAGS
AUG Leaders

Atlassian Community Events