Changes the parent ticket field based on the sub-task field
Asked by Lakshmi S:

Hi Team,

We are looking for the configuration to update the parent ticket field value based on the sub-task field value using the scriptrunner  

The requirement is :

If the main task has "n" number of sub-tasks, and if the "Scheduled Completion Date" of any subtask is changed to a later date(farthest), we should update the main task with that date.   

Ex : If subtasks "Scheduled Completion Date" are sub-task1 = June 23, sub-task2 =July 20, sub-task3 = July 26, etc., and a user changes any of these sub-tasks to August 12, the main ticket should also changes to August 12th.


Answers:
author: John Funk
body: Hi Lakshmi,

I do that with a simple Automation Rule:

![Screenshot.png](https://community.atlassian.com/t5/image/serverpage/image-id/333830i36E64D0A6F593D92/image-size/large?v=v2&px=999 "Screenshot.png")


author: Matt Parks
body: You could probably do this through an Automation rule, although I admit to not being super familiar with them, so I'm not sure how the logic would work to get the latest date.

If you wanted to use Scriptrunner, you would still probably want to have an Automation rule that triggers when the Scheduled Completion Date field is updated. The rule would trigger a custom event, but only when the field was updated on the particular subtask issue type (so it wouldn't trigger when the parent's value was updated).

I'm not sure what level of assistance you need with the Scriptrunner script, but something like this would do most of what you want. In this example, I'm using a field called "Target End", but you could change the name of the field you're looking for.

The last section updates the field on the parent, but doesn't generate an IssueUpdated event. It also re-indexes the parent issue so that the new field value will be used when doing any kind of JQL search.

import com.atlassian.jira.security.JiraAuthenticationContext;  
import com.atlassian.jira.issue.index.IssueIndexingService;  
import com.atlassian.jira.component.ComponentAccessor;  
import com.atlassian.jira.user.ApplicationUser;  
import com.atlassian.jira.issue.IssueManager;  
import com.atlassian.jira.issue.MutableIssue;  
import com.atlassian.jira.util.ImportUtils;  
import com.atlassian.jira.issue.Issue;  
import com.atlassian.jira.issue.index.IssueIndexingService;  
import com.atlassian.jira.event.type.EventDispatchOption;

<br />

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();  
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService);

def targetEndField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Target End").first()  
Date latestDate = null  
def targetEndValue  
def issue = event.issue  
def parentIssue = issue.getParentObject() as MutableIssue  
Collection\<Issue\> subtasks = parentIssue.getSubTaskObjects()

subtasks.each  
{  
targetEndValue = it.getCustomFieldValue(targetEndField) as Date  
if (!latestDate \&\& targetEndValue)  
{  
latestDate = targetEndValue  
return;  
}

if (targetEndValue \> latestDate)  
{  
latestDate = targetEndValue  
}  
}

parentIssue.setCustomFieldValue(targetEndField, latestDate)  
ComponentAccessor.getIssueManager().updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)  
boolean wasIndexing = ImportUtils.isIndexIssues();  
ImportUtils.setIndexIssues(true);  
issueIndexingService.reIndex(ComponentAccessor.getIssueManager().getIssueObject(issue.id));  
ImportUtils.setIndexIssues(wasIndexing);


Tags: behaviours

data-center

jira-data-center

scriptrunner