Missed Team ’24? Catch up on announcements here.

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

Scriptrunner for Jira cloud - Diference between datetime custom fields

José Matheus Neto October 29, 2019

All i wanna do is compare 2 custom datetime fields and show the difference in a custom numeric field (in hours if possible)

 

The closest i got is here:

 

import java.time.LocalDate
import java.time.LocalDateTime;

import static java.time.temporal.ChronoUnit.MINUTES


def reqagreeddate = LocalDateTime.parse(issue.fields.customfield_10029)
def resolutionDate = LocalDateTime.parse(issue.fields.customfield_10032)
def dateDifference = MINUTES.between(reqagreeddate, resolutionDate)


issueInput.fields.customfield_10031 = dateDifference

 

but i got the following ERROR 

2019-10-25 18:37:53.523 ERROR - Text '2019-10-25T00:00:00.000-0300' could not be parsed, unparsed text found at index 23

 

 

I resolved this by giving up and using an project automation plugin for jira called Automation for jira... Scriptrunner still very poor for cloud version in my opinion. With this new tool i was able to do all i wanted in less than 2h.

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2019

Hi Jose,

Thank you for your question.

I can confirm that the code suggested above will not work for Jira Cloud due to the fact that the code you have provided is foo ScriptRunner for Jira Server and this will not work as Atlassian only provide a rest API in Jira Cloud and do not provide a Java API in the cloud like they do in Jira Server.

You can see more detailed information on the differences between the cloud and server versions inside of our documentation page located here.

I can confirm that to create a calculated field that calculates the value of two custom fields and stores the value inside a third custom field with ScriptRunner for Jira Cloud that you should use a Script Listener which is configured to fire on the Issue Updated event. 

Finally, I can confirm that we have an example Script Listener script located in the documentation page here which shows how to create a calculated field and I can confirm that this should be used as a reference guide to help create the script which you require.

Kind Regards,

Kristian

José Matheus Neto October 29, 2019

Hi Kristian!

 

Thank's for your help! 

 

I was using the post-function ScriptRunner method, but i now see how i can use the Listeners and i can do much more stuff that i wanna do, the only problem is that i only was able to find INTEGER or STRING declarations on the documentation, so i still don't know how to convert a string field to datetime and then extract the time in minutes from it.

 

Thanks!

José

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2019

Hi Jose,

Thank you for your response.

I can confirm that to get the date field values and to set the date field values that you should refer to the examples which we have in the documentation here as these show how to extract the values from different field types on an issue. 

I can also confirm that to convert the string to a date/timestamp that you will need to use some of the standard groovy methods for parsing dates to do this and I can confirm the page here explains how to do this in more detail

Kind Regards,

Kristian

0 votes
Will October 29, 2019

Hi there,

 

I am sure there are some differences between server and cloud but I was able to get this running on the server console, see no reason why it wouldn't run on on a scripted field

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def PSDCF = customFieldManager.getCustomFieldObjectsByName("date1")[0]
def PEDCF = customFieldManager.getCustomFieldObjectsByName("date2")[0]
def PSDCFV = issue.getCustomFieldValue(PSDCF)
def PEDCFV = issue.getCustomFieldValue(PEDCF)

def DateDiff = PEDCFV - PSDCFV

return DateDiff * 24

 Let me know how you get on with that? you would need some if statements if any of the values were null.

José Matheus Neto October 29, 2019

Hey Will!

 

The code that you posted unfortunally doesn't work,

 

Thanks for the answer anyway!!

José

Will October 30, 2019

apologies, I didn't realise that Jira server and Jira cloud were so different!

anybody looking for the same problem on Jira server this code does work for Server.

TAGS
AUG Leaders

Atlassian Community Events