Need to create a groovy script and script runner

Anuradha Yadav
Contributor
December 17, 2020

Hi Team,

Please there is a requisite where user want to create a custom field which gives value to (current date – Due date ) and this value is to be used in the dash board with respect to the issues.

Basically we have an exsisting Jira project in server instance and we need a script to know the issue due date once the issue is created. This information should be displayed on the dashboard of the project.

Please help with the script.

 

Regards

Anu

2 answers

2 accepted

0 votes
Answer accepted
Radek Dostál
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.
December 17, 2020

You'd probably be looking for something similar to

/*
For console debug
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

IssueManager issueManager = ComponentAccessor.getIssueManager()
Issue issue = issueManager.getIssueObject("<ISSUEKEY>")
*/

Long currentTimeMillis = System.currentTimeMillis()
Long dueDateTimeMillis = issue.getDueDate()?.getTime()

if (currentTimeMillis && dueDateTimeMillis) {
return ((dueDateTimeMillis - currentTimeMillis) / (1000 * 60 * 60)) // millis to seconds, to minutes, to hours
} else return null

 

This will return a number, so your field also should return a number.

If you want to round this down, you can use the Math.round function similar to

return Math.round((Float) ((dueDateTimeMillis - currentTimeMillis) / (1000 * 60 * 60)))
Anuradha Yadav
Contributor
December 20, 2020

I need to set up this in the board, how can I do that? please help

Anuradha Yadav
Contributor
December 21, 2020

Hi @Radek Dostál

This code is returning NULL as value . but the problem is we cant go for each individual issue by using <ISSUEKEY> value . it should check for all the issue in the project with different status except closed .that should be the condition it should work with.

 

Could you please help!

 

Regards,

Anuradha

Radek Dostál
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.
December 21, 2020

I'm very confused by what you are trying to set up, where, and with what. Script fields automatically have $issue variable in the binding, so you do not need to explicitly assign the variable - that's why the whole IssueManager/Issue is commented out - that's just for testing purposes.

 

I was under the assumption you are adding a script field and then wanted to show that field as a column in a dashboard (filter results?). Can you clarify exactly what it is you want to set up, and what you have done so far?

Anuradha Yadav
Contributor
December 21, 2020

Im sorry if my info was not relevant.

We are trying to work on user request for the addition of script.

User have a project in jira and User wants to display dash board with respect to all the issues, he wants create a custom field which gives value to (current date – Due date ) and this value must be on the dashboard so that user can see what is the count of the issues on due coming week. so that he can work on due issues.

Anuradha Yadav
Contributor
December 21, 2020

this is the script I got from my colleague:

 

import java.sql.Timestamp
import java.time.temporal.ChronoUnit
//import com.atlassian.jira.issue.IssueManager
//import com.atlassian.jira.issue.Issue
//IssueManager issueManager = ComponentAccessor.getIssueManager()
//Issue issue = issueManager.getIssueObject("") // Get the required component
def customFieldManager = ComponentAccessor.customFieldManager
// The name of the lower date custom field
final String lowerDateCustomFieldName = "Lower Date Custom Field"
// The name of the higher date custom field
final String higherDateCustomFieldName = "Higher Date Custom Field" // Get the custom field objects
def lowerDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == lowerDateCustomFieldName }
def higherDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == higherDateCustomFieldName }
if (!lowerDateCustomField || !higherDateCustomField) {
log.info "Could not find one ore more of the provided custom fields"
return null
} // Get the date values from both issues
def lowerDateValue = issue.getCustomFieldValue(lowerDateCustomField) as Timestamp
def higherDateValue = issue.getCustomFieldValue(higherDateCustomField) as Timestamp
// Transform both values to instants
def lowerDateInstant = lowerDateValue?.toInstant()
def higherDateInstant = higherDateValue?.toInstant() // Change the chrono unit to obtain the difference in other time unit.
final chronoUnit = ChronoUnit.DAYS
// Calculate the difference between the lower and the higher date.
lowerDateInstant && higherDateInstant ? chronoUnit.between(lowerDateInstant, higherDateInstant) : null

 

 

 

anything can you relate to this for my user request?

Regards,

Anu

Anuradha Yadav
Contributor
December 21, 2020

image.png

This what user is expecting. For each app the count of due issues in weeks.

Radek Dostál
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.
December 22, 2020

Hi,

 

So are you adding a Script Field (https://scriptrunner.adaptavist.com/6.16.0/jira/scripted-fields.html) then?

 - what is the field you have created (note to limit context to avoid performance issues)

 - what is final code you placed into it

 - have you set a number searcher for the field

 - and have you set the field's template to be a Number

 - have you added the field as column in the dashboard gadget

 

I would advice you to check the Script Fields tutorial which contains all the information you need to set it up: https://scriptrunner.adaptavist.com/6.16.0/jira/tutorials/script-fields-tutorial.html

The code I provided is quite general and should work for any issue having Due date value - I did try it before I pasted here.

 

Regards,

Radek

Anuradha Yadav
Contributor
December 28, 2020

Hi,

After long search on web . I got the below:

We need to achieve this on our project dahsboard for the client. Without using JIRA Calendar plugin. We want  to achieve this by script. Please me if you have the script.

 

Thanks,

You help will be much appriciated.

Regards,

Anu

 

 

0 votes
Answer accepted
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 17, 2020

Hi @Anuradha Yadav 

I couldn't exactly understand the problem, sorry.

There is a Due Date system field in Jira. You can add this field to any screen you like. In order to set this field, users need "Schedule Issues" permission in project's permission scheme.

Apart from that, could you please explain why do you need a script?

Anuradha Yadav
Contributor
December 20, 2020

There is a due date field ok . Then i will add it in the screen and make it compulsory for the user to fill in the field . Then we will get due date after that how do we project it in the dashboard ?

Anuradha Yadav
Contributor
December 20, 2020

User need this info in the dashbaord, pls help how ca i do this

Suggest an answer

Log in or Sign up to answer