Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I want to write a ScriptRunner Listerner to automatically populate a field, if a field is updated.

Karl Samson
Contributor
June 15, 2022

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

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2022

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)
}
0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2022

What do you mean by "filter on the user that made the change"?

TAGS
AUG Leaders

Atlassian Community Events