Script listener: Copy specific field value from parent to each sub task

Hi

I would like to have a script listener which updates each sub-task if a specific field on their parent task has been updated.

Here's what I have so far.

import com.atlassian.jira.component.ComponentAccessor
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.DefaultUserManager

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
log.debug("--------------------This is to test Behaviours Logging----------------")

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTasksList = issue.getSubTaskObjects()
def type = issue.issueType.name

def changeHolder = new DefaultIssueChangeHolder()

def userManager = ComponentAccessor.getUserManager()
def tgtField = customFieldManager.getCustomFieldObjectByName("Projektleiter")

if(type == "Projekt")
{
    // Get list of modified values
    List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
    Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()
    
    String oldValue
    String newValue
    
    while (changeItemListIterator.hasNext())
    {
        GenericValue changeItem = (GenericValue)changeItemListIterator.next()
        String currentFieldName = changeItem.get("field").toString()
        
        if(currentFieldName == "Projektleiter")
        {
            oldValue = changeItem.get("oldstring")
            newValue = changeItem.get("newstring")
            
            if (oldValue != newValue)
            {
                log.debug("Field changed from: "+oldValue+" to "+newValue)
                
                ApplicationUser NewProjectLeader = userManager.getUserByName(newValue)
                
                for(subTask in subTasksList)
                {
                    tgtField.updateValue(null, subTask, new ModifiedValue(issue.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)
                    
                    log.debug(subTask)
                }
            }
        }  
    }
}

I'm able to say if the specific field actually has been updated and then also get the new value. I just can't get it to update the field on each sub-task.

Thanks for any help.

1 answer

This widget could not be displayed.

I would think that that the line where you're calling updatevalue() should use the old value of the subtask, not of the original issue. Perhaps try subtask.getCustomFieldValue(tgtField).

tgtField.updateValue(null, subTask, new ModifiedValue(subtask.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)

Hi Joshua

Thanks for your help so far. Unfortunately, it does not work. It just empties the field on each sub-task.

What type of custom field is Projektleiter? Is it a User Picker field?

Yes, exactly. Single User Picker.

Marius,

I believe your problem with the script is coming from this line:

ApplicationUser NewProjectLeader = userManager.getUserByName(newValue)

The problem with doing this is that newValue is NOT a username. newValue is their display name. You can't call getUserByName using someone's display name and there is no method for doing so. If you add log.debug(NewProjectLeader), you'll see that it is null.

Try the following script instead:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Level
import org.ofbiz.core.entity.GenericValue

log.setLevel(Level.DEBUG)
log.debug("--------------------This is to test Behaviours Logging----------------")

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTasksList = issue.getSubTaskObjects()
def type = issue.issueType.name

def changeHolder = new DefaultIssueChangeHolder()

def userManager = ComponentAccessor.getUserManager()
def tgtField = customFieldManager.getCustomFieldObjectByName("Projektleiter")

if(type == "Projekt")
{
// Get list of modified values
List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()

String oldValue
String newValue

while (changeItemListIterator.hasNext())
{
GenericValue changeItem = (GenericValue)changeItemListIterator.next()
String currentFieldName = changeItem.get("field").toString()

if(currentFieldName == "Projektleiter")
{
oldValue = changeItem.get("oldstring")
newValue = changeItem.get("newstring")

if (oldValue != newValue)
{
log.debug("Field changed from: " + oldValue + " to " + newValue)

def NewProjectLeader = issue.getCustomFieldValue(tgtField)

for(subTask in subTasksList)
{
tgtField.updateValue(null, subTask, new ModifiedValue(subTask.getCustomFieldValue(tgtField), NewProjectLeader),changeHolder)

log.debug(subTask)
}
}
}
}
}

This script worked me in a listener to update the subtask custom field value whenever the parent gets updated.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

534 views 3 19
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you