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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Update field when another field is changed

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

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?

Hi,

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

0 votes

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);
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

139 views 2 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you