The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hi there!
By using ScriptRunner I'm trying to compare the dates between two custom fields and save the result on another custom field (using the hour unit). Alhtough it seems to not be working :|
Here is the code:
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
final chronoUnit = ChronoUnit.HOURS
def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
// get custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
def input1CfId = customFields.find { it.name == 'Incident Start Time' }?.id
def input2CfId = customFields.find { it.name == 'Incident End Time' }?.id
def outputCfId = customFields.find { it.name == 'Incident Duration' }?.id
def projectKey = "PI"
if (issue == null || ((Map)issue.fields.project).key != projectKey) {
logger.info("Wrong Project ${issue.fields.project.key}")
return
}
def input1CfIddate = ZonedDateTime.parse(input1CfId, formatter)
def input2CfIddate = ZonedDateTime.parse(input2CfId, formatter)
if (input1CfIddate == null || input2CfIddate == null) {
logger.info("Calculation using ${input1CfIddate} and ${input2CfIddate} was not possible")
return
}
def output = chronoUnit.between(input1CfIddate, input2CfIddate)
if (output == (issue.fields[outputCfId] as String)) {
logger.info("already been updated")
return
}
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId): output + " " + chronoUnit.toString().toLowerCase()
]
])
.asString()
Any hints?
Thanks!
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
final chronoUnit = ChronoUnit.HOURS
def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
def issue = get("/rest/api/2/issue/TMAPSJIRA-1129")
.asObject(Map).getBody()
// get custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
def input1CfId = 'created' //customFields.find { it.name == 'Incident Start Time' }?.id
def input2CfId = 'resolutiondate' //customFields.find { it.name == 'Incident End Time' }?.id
def outputCfId = customFields.find { it.name == 'Incident Duration' }?.id
def projectKey = "TMAPSJIRA"
if (issue == null || ((Map)issue.fields.project).key != projectKey) {
logger.info("Wrong Project ${issue.fields.project.key}")
return
}
def input1CfIddate = ZonedDateTime.parse(issue.fields[input1CfId], formatter)
def input2CfIddate = ZonedDateTime.parse(issue.fields[input2CfId], formatter)
if (input1CfIddate == null || input2CfIddate == null) {
logger.info("Calculation using ${input1CfIddate} and ${input2CfIddate} was not possible")
return
}
def output = chronoUnit.between(input1CfIddate, input2CfIddate)
if (output == (issue.fields[outputCfId] as String)) {
logger.info("already been updated")
return
}
logger.info(output + " " + chronoUnit.toString().toLowerCase())
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId): output + " " + chronoUnit.toString().toLowerCase()
]
])
.asString()
Change :
def input1CfId = 'created' //customFields.find { it.name == 'Incident Start Time' }?.id
def input2CfId = 'resolutiondate' //customFields.find { it.name == 'Incident End Time' }?.id
And Update :
def input1CfIddate = ZonedDateTime.parse(input1CfId, formatter)
def input2CfIddate = ZonedDateTime.parse(input2CfId, formatter)
TO
def input1CfIddate = ZonedDateTime.parse(issue.fields[input1CfId], formatter)
def input2CfIddate = ZonedDateTime.parse(issue.fields[input2CfId], formatter)
Are there any logs to your result of script when you run it? Indicating why it's not working. You should probably log the value of ${input1CfIddate} and ${input2CfIddate}
OR
You can log out the value of output that way you can see what the results are at that point in your code and if the data structure is a datetime type or actually a string type. I believe it should be a datatime based on the initial declaration.
Having more insight might help understand what's causing it not to work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.