Hi Team,
I am trying to update a date field called "FTR" whenever another date field (FSD) is updated.
I am trying to do this using Automation for Jira (attached screenshot).
My script is as below
import com.atlassian.jira.component.ComponentAccessor import java.util.Calendar // Get the custom field manage def customFieldManager = ComponentAccessor.getCustomFieldManager() // Retrieve the custom fields by name def fsdField = customFieldManager.getCustomFieldObjectByName("FSD (First Service Date)") def ftrField = customFieldManager.getCustomFieldObjectByName("First Training Release Date") // Log if the fields are not found if (fsdField == null) { return "FSD (First Service Date) custom field not found." } if (ftrField == null) { return "First Training Release Date custom field not found." } // Get the value of the FSD field (date) def fsdDate = issue.getCustomFieldValue(fsdField) // Ensure fsdDate is not null if (fsdDate == null) { return "FSD (First Service Date) field is empty or invalid." } // Log the original FSD date for debugging log.info("Original FSD Date: " + fsdDate) // Create a Calendar object to manipulate the date def calendar = Calendar.getInstance() calendar.setTime(fsdDate) // Loop to subtract 28 business days (skip weekends) int businessDaysToSubtract = 28 int subtractedDays = 0 while (subtractedDays < businessDaysToSubtract) { calendar.add(Calendar.DAY_OF_MONTH, -1) // Go back one day // Check if it's a business day (Monday to Friday) if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { subtractedDays++ } } // Get the new date after subtracting 28 business days def newDate = calendar.getTime() // Log the new date for debugging log.info("New Date after subtracting 28 business days: " + newDate) // Set the calculated date to the First Training Release Date field issue.setCustomFieldValue(ftrField, newDate) // Log success log.info("Successfully updated First Training Release Date field to: " + newDate) // Return success message return "First Training Release Date successfully updated to: ${newDate}"
But it is not updating the FTR field.
Am i missing something here?
Since the FTR field is dependent on another field's value, why not use Behaviour or Listener instead of Automation?
Behaviours and Listeners are much simpler to manage than Automation and can do the exact same thing.
If you want to do this via Behaviour, you will need to create a Server-Side Behaviour.
Below is a sample working code for your reference:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleDate = getFieldById(fieldChanged)
def baselineDate = getFieldByName('Baseline Date')
if (sampleDate.value) {
def sampleDateValue = sampleDate.value as Date
def updatedDate = (sampleDateValue + 5).format('dd/MM/yy')
baselineDate.setFormValue(updatedDate)
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.
Below is a screenshot of the Behaviour configuration:-
If you would prefer to use the Listener, below is a sample working Listener code for your reference:-
def issue = event.issue
def sampleDateValue = issue.getCustomFieldValue('Sample Date') as Date
if (sampleDateValue) {
issue.update {
setCustomFieldValue('Baseline Date', sampleDateValue + 5)
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.
Below is a screenshot of the Listener configuration:-
I hope this helps to answer your question. :)
Thank you and Regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_
Thank you for your advise. I cannot use behaviours because FTR field(other field ) will not be present on the edit screen. It will be only on view screen.
I tried to use listeners with below script but the value is not being set on the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
After reviewing your code, I can confirm it will not work.
You keep making the mistake of adding return messages at the very bottom of the code:-
return "FTR Date has been set to: " + formattedFtrDate
This will not work. You must only return a date value, e.g. 27/Dec/24, or according to the date format you have set in your Jira instance.
The date field will not recognize values like "FTR Date has been set to: ". Hence, the error message it is returning is expected.
Please clarify: Do you intend for the second date field to return the next working date? If yes, how many days should there be between the two date fields?
I suggest modifying your Listener code to something like this:-
import java.time.ZoneId
def issue = event.issue
def sampleDateValue = issue.getCustomFieldValue('Sample Date') as Date
if (sampleDateValue) {
issue.update {
setCustomFieldValue('Baseline Date', getNextBusinessDay(sampleDateValue))
}
}
def getNextBusinessDay(Date dateToConvert) {
def localDate = dateToConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
def updatedDate
if (localDate.dayOfWeek.toString() == 'FRIDAY') {
updatedDate = localDate.plusDays(3) //Update the value of the days
} else if (localDate.dayOfWeek.toString() == 'SATURDAY') {
updatedDate = localDate.plusDays(2) //Update the value of the days
} else if (localDate.dayOfWeek.toString() == 'SUNDAY') {
updatedDate = localDate.plusDays(1) //Update the value of the days
} else {
updatedDate = localDate.plusDays(1) //Update the value of the days
}
java.sql.Date.valueOf(updatedDate).format('d/MMM/yy')
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.
I am looking forward to your feedback and clarification.
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.
Hi @Ram Kumar Aravindakshan _Adaptavist_
I have removed return statement at the end and just made as
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vineela,
Could you please share the use case here. If there is no other actions then you can use smart values to update the field, instead of script!! Is it anything blocking that!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.