Hi Experts,
I am trying to populate a Date field by Adding days to another date field value.
Consider, there are two date fields "Date1" & "Date2" and a numeric field "Number Field" for days.
Required Output : Date2 = Date1+NumberField
How can I achieve this with the groovy script?
Regards,
Pavithra D
Community moderators have prevented the ability to post new answers.
Hi Pavithra
You can use a scripted field for the Date2, which will be 'connected' to Date1 and the value of the number field. The script that calculates the Date2 will be something like
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue Issue issue = issue def customFieldManager = ComponentAccessor.getCustomFieldManager() def daysToAddCF = customFieldManager.getCustomFieldObjectByName("Number Field") def dateACF = customFieldManager.getCustomFieldObjectByName("Date A") def dateAValue = issue.getCustomFieldValue(dateACF) as Date def daysToAdd = issue.getCustomFieldValue(daysToAddCF) as int def dateB = Calendar.getInstance() dateB.setTime(dateAValue) dateB.add(Calendar.DATE, daysToAdd) return dateB.getTime()
So every time you change one of the Number Field or Date A fields the Date B field will be updated automatically.
Regards
Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
In the date time script field how to truncate the time? And is there any way to do this in the normal date field?
Thanks,
Pavithra D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is there a way to subtract instead of add?
"dateB.subtract(Calendar.DATE, daystoAdd)" did not work for me.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
it looks like "Jira Misc Custom Fields" this add one can easy to do the calculation and doesn't need to save the value in DB. but does any know how to create the code?
thanks.
https://marketplace.atlassian.com/plugins/com.innovalog.jmcf.jira-misc-custom-fields/server/support
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I tried to adjust the code you posted for my specific requirement which is to have a static number instead of number Custom field but I am getting 'Invalid Date' in the field where the result should be. Could you please have a look at the code and tell me where the problem is?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
int daysToAdd = 10
def FirstDate = customFieldManager.getCustomFieldObjectByName("FirstDate")
def dateAValue = issue.getCustomFieldValue(FirstDate) as Date
def Calculated = Calendar.getInstance()
Calculated.setTime(dateAValue)
Calculated.add(Calendar.DATE, daysToAdd)
return Calculated.getTime()
Thanks,
Flan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Flan,
It should have just worked.
What kind of searchers and tempate you use for the scripted field ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thanos,
Searcher: Free Text Searcher
Template: Date Time Picker
Thanks,
Flan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Flan,
Try with a date picker searcher instead.
regards Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It worked perfectly! Thank you very much Thanos :)
Another question please: Is it possible to do that with months?
i.e. Datefield2 = Datefield1 - 10 months
This is the code I am trying to do that with (not working at all):
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.text.DateFormat
import groovy.time.TimeCategory
import java.util.Date
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def FDate = customFieldManager.getCustomFieldObjectByName("FirstDate")
def dateAValue = issue.getCustomFieldValue(FDate) as Date
def Calculated = Calendar.getInstance()
def tDate = new Date()
use (groovy.time.TimeCategory){
tDate = dateAValue - 10.months
Calculated.setTime(tDate)}
return Calculated.getTime()
Thank you again in advance,
Flan
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.