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)
@PD Sheehan
What do you mean by "filter on the user that made the change"?
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.ComponentAccessorimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.util.ImportUtilsimport com.atlassian.jira.event.type.EventDispatchOptionimport com.atlassian.jira.issue.index.IssueIndexingServiceimport org.apache.log4j.Leveldef changeLog = event.changeLogdef issue = event.issue as MutableIssuedef 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.ComponentAccessorimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.util.ImportUtilsimport com.atlassian.jira.event.type.EventDispatchOptionimport com.atlassian.jira.issue.index.IssueIndexingServiceimport org.apache.log4j.Leveldef changeLog = event.changeLogdef issue = event.issue as MutableIssuedef 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)}
It looks like you're new here. Sign in or register to get started.