I need a basic thing from script runner, but i have no idea how to do it.
All i need is for a custom field in an issue to be updated with a value depending on the value of 2 other fields.
Example: If field A = X, and field B = Y, enter value 4 in custom field.
or, If field A = V, and field B = W, enter value 5 in custom field.
There will be quite a few entries and there are loads of values i have to be entered, but once ive got the general idea of how it works, i can do the rest.
Thanks
Hi @Shah Baloch
For your requirement, you could try something like this:-
import com.atlassian.jira.component.ComponentAccessor
import java.util.concurrent.TimeUnit
def customFieldManager = ComponentAccessor.customFieldManager
def sampleDate = customFieldManager.getCustomFieldObjectsByName('Sample Date').first()
def sampleDateValue = issue.getCustomFieldValue(sampleDate) as Date
def currentDate = new Date(System.currentTimeMillis())
if (sampleDateValue) {
def dateBeforeInMs = sampleDateValue.time
def dateAfterInMs = currentDate.time
def timeDiff = Math.abs(dateAfterInMs - dateBeforeInMs)
TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS)
}
Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Scripted Field configuration:-
As shown in the screenshot above, the Scripted Field type used is the Number Field.
Also, I include a few test screenshots for your reference:-
1. First, I create a new issue and set a date for the Sample Date field. For this example, the date field has been set to 1st September 2022.
2. Once the ticket is created, as expected, the Scripted Field calculates the difference between the date set in the Sample Date field to the Current date (10th September 2022).
As expected, the result displayed in the Scripted Field is 9.
I hope this helps to solve your question. :)
Thank you and Kind regards,
Ram
Hi, @Ram Kumar Aravindakshan _Adaptavist_ it works thank you. I tried to do it on the opposite but still got the same positive numbers. I thought it'll show negative. I mean try to minus the current date from the deadline custom date. For example, the deadline date is 01/Sep and the Current date is 12/Sep. I was expecting to see -11 instead of 11. Here is the change I made.
def timeDiff = Math.abs(dateBeforeInMs - dateAfterInMs)
Thank you for your help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
Glad to hear the solution worked for you. :)
Please accept the answer.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_My apology didn't explain it well. I was trying to ask if is it possible to take out a higher number from a smaller number. I mean I just want to have the difference between positive and negative. For example, if the deadline is in the future the positive number but the deadline date is past then should display a negative number. In that way, we know how many days we were late from the deadline. For example, if the deadline was 01/Sep and today is the 12th then the number should be negative but if the deadline is 23/Sep and today is the 12th then the number should be positive. In your given code regardless of the change order of the timeDiff code it still shows a positive number.
How can I acheive that?
Thank you for your help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
If you want to return a negative value for dates that have exceeded the due date, you can just add a basic multiplication of the result with -1. For example:-
import com.atlassian.jira.component.ComponentAccessor
import java.util.concurrent.TimeUnit
def customFieldManager = ComponentAccessor.customFieldManager
def sampleDate = customFieldManager.getCustomFieldObjectsByName('Sample Date').first()
def sampleDateValue = issue.getCustomFieldValue(sampleDate) as Date
def currentDate = new Date(System.currentTimeMillis())
if (sampleDateValue) {
def dateBeforeInMs = sampleDateValue.time
def dateAfterInMs = currentDate.time
def timeDiff = Math.abs(dateAfterInMs - dateBeforeInMs)
def result = TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS)
if (dateAfterInMs > dateBeforeInMs) {
result * -1
} else {
result
}
}
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are a few test screens for your reference:-
1. If the Date in the date field has been set for a later date, the result will be a positive number, as shown below:-
2. And if the date in the date field has been set for an earlier date, the result will be a negative number, as shown below:-
I hope this helps to answer your question. :)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_
I was hoping you could help with a similar query . I am using your script to achieve the same thing but was wondering if there was a way to use it within a normal field where in a scheduled job could run each day to update the value of the field ( to accurately display the current date - custom date ) with the issues displayed on a dashboard . Do you know of a way I can achieve this ?
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So, the scenario is pretty much the same. In your case, you will only need to use either the Custom Scheduled Job or Escalation Service, but you will need to specify which field you want to update, i.e., which Date field it is.
I suggest creating a separate question for this in the community and let me know by just adding an attn: to me so I can look into it.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.