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
Hello,
Do you have Cloud version or Server?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
by custom field you mean the available date picker fileds rights ? or should i create my own custom fields..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes, available date picker fields
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
cool, where should i add my script. should i write my scriot on the script listener or some where else ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
into post function. open desired transition and add scripted post function
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am getting an error "the variable [issue] is undeclared.
How should i handle this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.