I've set up a listener in scriptrunner to add 5 days to a date picker field every time the assignee field is changed. It isn't working...and I'm not sure where the failure is. The events it fires on are, Issue Created, Issue Updated, Issue Assigned. It is not failing, but it also isn't doing what I need it to do.
Is a listener a viable way to make this work? I didn't think post functions would, so I was hoping a listener could do the job.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import java.util.Date
import java.text.SimpleDateFormat
import java.util.Calendar
def issue = event.issue as MutableIssue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find{ it.field == "assignee"}
// check if the "assignee" field was changed
if (change) {
// get the custom field object by name
def baselineEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Baseline End")
// check if the custom field is present on the issue and has a value
if (issue.getCustomFieldValue(baselineEndField) instanceof Date) {
// get the current value of the custom field
Date baselineEnd = issue.getCustomFieldValue(baselineEndField)
// add 5 days to the custom field
Calendar calendar = Calendar.getInstance()
calendar.setTime(baselineEnd)
calendar.add(Calendar.DATE, 5)
Date newBaselineEnd = calendar.getTime()
// set the new value of the custom field
issue.setCustomFieldValue(baselineEndField, newBaselineEnd)
}
}
Hi Ashley!
Seems like you are missing the logic to persist the change to the issue. Your last line is just setting the value on the issue object, you still have eto persist using the issuemanager (which you also need to import and declare):
import com.atlassian.jira.event.type.EventDispatchOption
...
...
def issueManager = ComponentAccessor.getIssueManager()
try {
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
} catch(Exception e) {
log.error("ERROR: Updating $issue.key")
}
Still to retrieve here is the user performing the update.
Hope this helps!
Thank you, I'm still learning how to do all of this - adding what you've put, would I do it like this?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import java.util.Date
import java.text.SimpleDateFormat
import java.util.Calendar
import com.atlassian.jira.event.type.EventDispatchOption
def issue = event.issue as MutableIssue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find{ it.field == "assignee"}
// check if the "assignee" field was changed
if (change) {
// get the custom field object by name
def baselineEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Baseline End")
// check if the custom field is present on the issue and has a value
if (issue.getCustomFieldValue(baselineEndField) instanceof Date) {
// get the current value of the custom field
Date baselineEnd = issue.getCustomFieldValue(baselineEndField)
// add 5 days to the custom field
Calendar calendar = Calendar.getInstance()
calendar.setTime(baselineEnd)
calendar.add(Calendar.DATE, 5)
Date newBaselineEnd = calendar.getTime()
// set the new value of the custom field
issue.setCustomFieldValue(baselineEndField, newBaselineEnd)
def issueManager = ComponentAccessor.getIssueManager()
try {
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
} catch(Exception e) {
log.error("ERROR: Updating $issue.key")
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
As I said, you see that the issueManager.updateIssue takes a user parameter, so you will have to get a user to provide to this method.
To get the current logged in user, triggering the script, you can use this:
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Good luck!
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.