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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Using Scriptrunner for Jira to calculate a date custom field based on another date custom field

Hi

We have a custom field date (Deadline) that is populated by a user and another custom field date (Sample Delivery Date) that we would we like Scriptrunner to auto populate with a 2 weeks (14 days) 'before' Deadline date value.

For example: I add 15/07/21 as my 'Deadline' date and SR auto populates/calculates value of 'Sample Delivery Date' as1/07/21.

I would like this relationship to be dynamic - the Deadline date could be added on Issue creation or changed later and the Sample Delivery Date should update automatically.

I am a newbie with SR runner but imagine this would be done as a behavior. 

Any help very much appreciated!

Thanks

Gordon

1 answer

1 accepted

Hi @Gordon Rutherford

For your requirement, the best approach will be to use the Scripted Field.

In your scenario, the Sample Delivery Date will be the Scripted Field.

Below a working sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Deadline")[0]

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

deadlineValue + 14

Below is a print screen of an example Scripted Field configuration for your reference.

scripted_field_config.png

If you view the print screen above, the field name given is Sample Delivery Date. And, the script takes the value from the Deadline field and adds 14 days to it.

Please note that the scripted field will only be visible once the issue has been created, i.e. you will not see the update on the Create screen or the Edit screen.

Below are a few test print screens.

1) When the ticket is being created, a date is entered into the Deadline field. In the print screen below, the date selected is 15th July 2021.

test1.png

2) Once the ticket is created, the Sample Delivery Date field is visible with the date value of 29th/Jul/21, i.e. 14 days after the Deadline.

test2.png

3) If the Deadline is updated, the Sample Delivery Date field is updated automatically, as shown in the images below:-

test3.pngtest4.png

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

@Ram Kumar Aravindakshan _Adaptavist_ 

 

Brilliant! Works perfectly as expected thank you!

 

Sorry to complicate it further but......

I have shown it to the team who requested the feature and in typical fashion they added a further request - could you have a condition added to this essentially based on a selection from a Radio Button custom field as described below. 

We have 4 additional radio buttons on this issue create screen.

Button A

Button B

Button C

Button D

If the user choose Button A or Button B the sample delivery date - 14 days from deadline

or 

If the user chooses Buton C or Button D the sample delivery date - 7 day from Deadline

 

Thanks again for your input - really appreciate it Ram

Best regards

Gordon

Hi @Gordon Rutherford

To answer your question, it is very much doable.

You will need to modify the code slightly. Below is a sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Deadline")[0]

def sampleRadioButton = customFieldManager.getCustomFieldObjectsByName("Sample Radio Button")[0]
def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).value.toString()

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

if(sampleRadioButtonValue == "Option1") {
deadlineValue + 14
} else if(sampleRadioButtonValue == "Option2") {
deadlineValue + 21
} else if(sampleRadioButtonValue == "Option3") {
deadlineValue + 28
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to modify it accordingly.

Below are some test print screens for your reference:-

First, when I create the ticket, I select Option1.

image1.png

When the ticket is created, it sets an additional 14 days to the Sample Delivery Date field as shown below. 

image2.png

 

Next, once the radio button option is updated to Option2, the Sample Delivery Date field is also updated, i.e. this time from 14 days to 21 days as shown in the image below:-
image3.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram

Fantastic - thankyou again! I have had it working with my code shown below in the snip. However why does it show problem on line 8? Can it be ignored?

Best regards

Gordon

Line 8 error.PNG

Hi @Gordon Rutherford

It's complaining due to the static type checker.

You can modify it slightly, i.e. from 

def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).value.toString()

to

def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).toString()

and this should resolve the issue.

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Hi Ram

Perfect - that solved it - one last question. We also use Confluence and represent Jira data information in tables on a Confluence page based on a Jira filter. The dates for these new scripted fields we have created present as shown below in the snip with 'extra' time information we don't need - any thoughts on that?

Cheers

SR dates.PNG

Hi @Gordon Rutherford

If you want to remove the timestamps from the date fields, the only alternative would be to change the Scripted Field Template type that you are now using, i.e. Date to Text Field (multi-line)

Change it from Date type, i.e.:-

date_type.png

to Text Field (Multi-Line) type, i.e.:-

text_type.png

Also, if you observe in the second print screen, there is a minor modification to the code. Earlier, the code was returning the value in Date format. This needs to be converted to the String format using the SimpleDateFormat.

Below is the updated code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Deadline")[0]

def sampleRadioButton = customFieldManager.getCustomFieldObjectsByName("Sample Radio Button")[0]
def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).toString()

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

if(sampleRadioButtonValue == "Option1") {
sdf.format(deadlineValue + 14)
} else if(sampleRadioButtonValue == "Option2") {
sdf.format(deadlineValue + 21)
} else if(sampleRadioButtonValue == "Option3") {
sdf.format(deadlineValue + 28)
}

 Below was the output in the Confluence using the Date type and previous code:-

confluence_old.png

Below is the updated output in Confluence after using the Text Field (multi-line) type and using the updated code:-

confluence_2.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram

Before I have had a chance to look at your last answer I am seeing some weird behavior -I must have caused but I don't know why.

I I did was change the Live Date (date picker CF) to Shoot Date Start (date picker CF)

 

This code

 

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

deadlineValue -70

 

now it does not generate a 'date' as below when template is date

Could you advise please

 

Thanks

 

Gordon

 

Thurs 1.PNG

Hi @Gordon Rutherford,

I have tested your code in my environment, and it works fine.

What I suspect is the searcher type that your scripted field is configured to is not of Date type.

Below is a print screen for your reference:-

scripted_field_config.png

If you notice in the highlighted section on the image above, the searcher type used for the Date field is Free Text Search.  You will need to update this to either Date Time or just leave it to None.

I am looking forward to your feedback.

Thank you and Kind Regards,

Ram

Hi Ram

Its is a Date Picker 

Regards

Gordon

Hi @Gordon Rutherford,

I have tested your code in my environment, and it works fine.

What I suspect is the searcher type that your scripted field is configured to is not of Date type.

Below is a print screen for your reference:-

scripted_field_config.png

If you notice in the highlighted section on the image above, the searcher type used for the Date field is Free Text Search.  You will need to update this to either Date Time or just leave it to None.

I am looking forward to your feedback.

Thank you and Kind Regards,

Ram

Hi Ram

OK you were right it was the searcher type that was causing the issue - set to none now so thankyou!

So I have returned now to getting the 'simple' date in the Confluence view.

I can't actually see the minor difference in code you refer to above to make this work - could you just supply that line of code or highlight it in a screen shot?

Thanks

Gordon

Hi @Gordon Rutherford

As I have explained in my previous comment, to display the Date correctly in Confluence, you will need to modify the Scripted Field type from Date to Text Field (Multi-Line). If you do not do this, the Date will display correctly on Jira, but it will show the full Date and Timestamp on Confluence.

Please follow the steps below to modify your Scripted Field configuration as well as your code.

1. First modify the Scripted Field's template from Date type i.e. 

date_type.png

to Text Field (Multi-Line) type as shown below:-

text_type.png

2) Next, you will need to modify your code.

Your current code returns the value as a Date i.e.

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

deadlineValue -70

You will need to modify it so that it returns the value as a String by changing the return statement that you are now using, i.e. from

deadlineValue -70

to

sdf.format(deadlineValue -70)

Below is the updated code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

sdf.format(deadlineValue -70)

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram

OK that works as you describe thankyou - however one last question is it possible for these date formats to match the standard Jira date formats in both Jira and Confluence?

10 Weeks to Shoot is currently set as 'yyyy-MM-dd'  but of course doesn't match the Jira Date format.

 

Thanks

 

Gordon

 




 

 

date.PNG

Hi @Gordon Rutherford

If you want the date format to match in both Jira and Confluence, then you will need to modify the format in the code, i.e. from:-

def sdf = new SimpleDateFormat("yyyy-MM-dd")

to

def sdf = new SimpleDateFormat("dd/MMM/yy")

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

Hi Ram

Sorry I just realised that the date returned is not correct in the screen snip above - its coming up as 03/12/17 when the code is supposed to be  70 days before 'Shoot Start date' which is 12/08/21 as shown below

Can you explain what's wrong there before I address the next point

Thanksdate 2.PNG

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sdf = new SimpleDateFormat("dd-MM-yyyy")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

deadlineValue -70

Hi @Gordon Rutherford,

Could you please include a log parameter to see what is the actual value in your custom field before you do the subtraction and share the log output that you receive?

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

log.warn "===>>> ${issue.getCustomFieldValue(deadline).toString()}"

def sdf = new SimpleDateFormat("dd-MM-yyyy")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())

log.warn "<<<===== ${deadlineValue - 70}"

deadlineValue -70

I noticed in the print screen you have shared in your previous comment, the date is displayed as 03-12-0017.

 

Thank you and Kind Regards,

Ram

Hi Ram

As requested

2021-07-12 11:26:53,544 WARN [runner.ScriptBindingsManager]: ===>>> 2021-08-12 00:00:00.0 2021-07-12 11:26:53,544 WARN [runner.ScriptBindingsManager]: <<<===== Fri Dec 03 00:00:00 GMT 17

Hi @Gordon Rutherford

Thank you for providing the logs.

Could you please modify the code slightly to:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sdf = new SimpleDateFormat("dd-MM-yyyy")

def deadlineValue = issue.getCustomFieldValue(deadline) as Date

sdf.format(deadlineValue -70)

And see if the output displayed for the date field is correct? I have tested this in my environment, and it seems to work correctly.

The result in confluence will follow the same format, i.e.:-

confluence_output.png

I am looking forward to your feedback.

Thank you and Kind Regards,

Ram

Hi Ram

Thanks for you help and yes that works as expected now!

Could you lastly advise on the below and then I will leave you in peace!

So of course the last part is to get the code working with the same date format for the other scripted field - the code below will need a modification too no doubt:

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sampleRadioButton = customFieldManager.getCustomFieldObjectsByName("Shoot Calendar Category")[0]
def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).toString()


def sdf = new SimpleDateFormat("yyyy-MM-dd")
def deadlineValue = sdf.parse(issue.getCustomFieldValue(deadline).toString())


if(sampleRadioButtonValue == "Home") {
deadlineValue - 14
} else if(sampleRadioButtonValue == "Mixed") {
deadlineValue - 14
} else if(sampleRadioButtonValue == "Beauty") {
deadlineValue -7
} else if(sampleRadioButtonValue == "Fashion") {
deadlineValue -7
}

Hi @Gordon Rutherford

It's nearly the same my last comment, i.e. you need to modify the code like below:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def customFieldManager = ComponentAccessor.customFieldManager
def deadline = customFieldManager.getCustomFieldObjectsByName("Shoot Start Date")[0]

def sampleRadioButton = customFieldManager.getCustomFieldObjectsByName("Shoot Calendar Category")[0]
def sampleRadioButtonValue = issue.getCustomFieldValue(sampleRadioButton).toString()

def sdf = new SimpleDateFormat("dd-MM-yyyy")

if(sampleRadioButtonValue != "null" && issue.getCustomFieldValue(deadline).toString()!= "null") {
def deadlineValue = issue.getCustomFieldValue(deadline) as Date

if (sampleRadioButtonValue in ["Home","Mixed"]) {
sdf.format(deadlineValue - 14)
} else if (sampleRadioButtonValue in ["Beauty","Fashion"]) {
sdf.format(deadlineValue - 7 )
}
}

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram

That works thankyou - I modified the date format so it displays the same in Confluence to dd/MMM/yy

However in Jira I have noted the other field '10 weeks etc' no longer appears as a date, where as 'sample arrived etc' does - any idea why that would be?

date 3.PNG

Hi Ram

Now the two fields are both on the left - it seems it took a while to ripple through to Jira.

Can we have them on right with the other dates?

date 4.PNG

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.16.1
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

33 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you