Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,979
Community Members
 
Community Events
184
Community Groups

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

Edited

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)

@Peter-Dave Sheehan

2 answers

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 15, 2022

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events