The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I need to write a script in our ScriptRunner Listener, to detect if the 'End date' field has been updated, and then populate another Date Picker field with the date that it was updated. Also if possible, to be able to filter on the user that made the change? (Currentuser)
Here is a sample listener script that looks for changes in the "End Date" field and updates the "Another Date Picker" field with the same date found in the End Date field.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import org.apache.log4j.Level
def changeLog = event.changeLog
def issue = event.issue as MutableIssue
def sourceFieldName = 'End Date'
def targetFieldName = 'Another Date Picker'
if (changeLog) {
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeItems = changeHistoryManager.getChangeHistoryById(changeLog.id as Long).changeItemBeans
//check if End Date is included in any of the changeItems
if (changeItems.any { it.field.equalsIgnoreCase(sourceFieldName) }) {
def changeItem = changeItems.find { it.field.equalsIgnoreCase(sourceFieldName) }
//be careful here, if you have more than one field by this name, you either need to filter further or replace with fieldId
def targetCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(targetFieldName)[0]
def sourceCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(sourceFieldName)[0]
def sourceValue = issue.getCustomFieldValue(sourceCf)
issue.setCustomFieldValue(targetCf, sourceValue)
updateIssueAndReIndex(issue)
log.debug("Set '$targetFieldName' to value from '$sourceFieldName' ($sourceValue) on issue $issue.key")
} else {
log.trace("$sourceFieldName for ${issue} was not changed")
}
} else {
log.trace("No changelog for the event")
}
def updateIssueAndReIndex(MutableIssue issue) {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//save the issue back to JIRA store - don't dispatch a new event, we are already in the middle of an even
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
//update the JIRA index
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
But you say "date that it was updated". Do you mean the current date?
If so, then this script will be better (assuming the other field is a date/time picket.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import org.apache.log4j.Level
def changeLog = event.changeLog
def issue = event.issue as MutableIssue
def sourceFieldName = 'End Date'
def timestampFieldName = 'Another Date Picker'
if (changeLog) {
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeItems = changeHistoryManager.getChangeHistoryById(changeLog.id as Long).changeItemBeans
//check if End Date is included in any of the changeItems
if (changeItems.any { it.field.equalsIgnoreCase(sourceFieldName) }) {
def changeItem = changeItems.find { it.field.equalsIgnoreCase(sourceFieldName) }
//be careful here, if you have more than one field by this name, you either need to filter further or replace with fieldId
def timestampCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(timestampFieldName)[0]
def newTimestamp = new Date().toTimestamp()
issue.setCustomFieldValue(timestampCf, newTimestamp)
updateIssueAndReIndex(issue)
log.debug("Set '$timestampFieldName' to current date/time ($newTimestamp) on issue $issue.key because $sourceFieldName was changed")
} else {
log.trace("$sourceFieldName for ${issue} was not changed")
}
} else {
log.trace("No changelog for the event")
}
def updateIssueAndReIndex(MutableIssue issue) {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//save the issue back to JIRA store - don't dispatch a new event, we are already in the middle of an even
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
//update the JIRA index
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
What do you mean by "filter on the user that made the change"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.