Update field when another field is changed

kukabgd June 24, 2021

Hi,

I would like to create a listener that is updating a date picker field ("review date") in subtask, whenever date picker field in project ("end of project") is changed.

The updated value of "review date" field should be = "End of project" - 1 month.

However I'm not sure how to do this, I would really appreciate it if someone could help me with this. 

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.
June 25, 2021

With a custom listener in script runner, you can change the subtask fields based on a parent issue change like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.MutableIssue

def triggerDateFieldName = 'end of project' //adjust to match case-sensitive name
def targetDateFieldName = 'review date' //adjust to match case-sensitive name
def offsetDays = -30

def issue = event.issue as MutableIssue

//get the list of changes associated with the event
def changeItems = ComponentAccessor.changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans

//Check that the triggerFieldName was part of the changes on the parent ticket
if( changeItems.any { it.field == triggerDateFieldName && it.fromString != it.toString} ) {
//get the actual date value (easier than parsing the change history string value imo
def triggerDateCf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(triggerDateFieldName )
def triggerDateVal = issue.getCustomFieldValue(triggerDateCf) as Date
//get the offset date
def targetDate = triggerDateVal + offsetDays

def targetDateCf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(targetDateFieldName )

//update all the subtasks ... you might need to apply some fittering if you don't want to update them all
issue.subTaskObjects.each{subTaskIssue ->
def subTask = subTaskIssue as MutableIssue
subTask.setCustomFieldValue(targetDateCf, targetDate)
updateIssueAndReIndex(subTask)
}

}


def updateIssueAndReIndex(MutableIssue issue) {
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//Put JIRA in indexing mode
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
//save the issue back to JIRA store
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
//update the JIRA index
indexingService.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.
June 24, 2021

You didn't tag your post with scriptrunner or Adaptavist... is this something that's available to you?

Without that or some other automation tool, I'm not aware of a native way to have a listener,

So can you start by letting us know what apps you have available in your environment?

kukabgd June 25, 2021

Hi,

thanks for the hint. I have just added scriptrunner tag. Yes I want to do this with scriptrunner.

Suggest an answer

Log in or Sign up to answer