Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I want to update the Assignee field from Parent to Subtask. When i update parent assignee field it s

Sai Krishna Yadav Challendula September 2, 2021

I want to update the Assignee field from Parent to Subtask. When i update parent assignee field it should update all subtask assignee field.

import com.atlassian.jira.issue.Issue
//import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

Issue issue = event.issue

def cwdUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def currentAssigneeId = issue.getAssigneeId()
log.warn("currentAssigneeId")
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == currentAssigneeId};
log.warn("childChangeItem")
//def currentAssigneeIdValue = change.currentAssigneeIdValue as String
def currentAssigneeIdValue = issue.currentAssigneeIdValue() as String
log.debug "new value is : ${currentAssigneeIdValue} "
if (change) {

//def currentAssigneeIdValue = change.currentAssigneeIdValue as String
// log.debug "new value is : ${currentAssigneeIdValue} "

issue.getSubTaskObjects()?.
findAll { it.issueType.name in ["Sub-task"]
//get only issues that their issue type is Sub-task

def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key)
subtask.setAssigneeId(currentAssigneeIdValue)
log.warn("Assignee----")
ComponentAccessor.getIssueManager().updateIssue(cwdUser, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)

}
}

Please help on this script.

Thanks,

Sai

3 answers

0 votes
Leo
Community Champion
September 7, 2021

Hi @Sai Krishna Yadav Challendula

Below is the working script to update sub-task assignee if parent assignee changed (to valid user)

this needs to be placed in Script listener and "Issue Updated" event needs to be mapped for the same

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.type.EventDispatchOption;

def issue = event.issue
def assigneeChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "assignee"}
if(assigneeChanged){
def assignee = issue.assignee
if(assignee){
issue.subTaskObjects.each { s ->
MutableIssue sb = s as MutableIssue
sb.setAssignee(assignee)
ComponentAccessor.getIssueManager().updateIssue(assignee, sb, EventDispatchOption.ISSUE_UPDATED, true)
}
}
}

I've tested the same in my sandbox and working as expected

 

BR,

Leo

0 votes
Sai Krishna Yadav Challendula September 7, 2021

No luck with above script. I have tried different script.

0 votes
Tuncay Senturk
Community Champion
September 2, 2021

Hi @Sai Krishna Yadav Challendula 

In your code, there is a line as below:

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == currentAssigneeId};


I am not sure but, can you please try

it.field == "Assignee" rather than it.field == currentAssigneeId   ?

Sai Krishna Yadav Challendula September 2, 2021

Hi @Tuncay Senturk ,

I have tried it.field == "Assignee" but it doesn't works for me . Please guide me.

 

Thanks in advance. Appreciate your help.

Tuncay Senturk
Community Champion
September 2, 2021

How about using it.field == "assignee"  , with lowercase "a"

Tuncay Senturk
Community Champion
September 2, 2021
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

Issue issue = event.issue

def cwdUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def currentAssigneeId = issue.getAssigneeId()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "assignee"};
def currentAssigneeIdValue = issue.currentAssigneeIdValue() as String
if (change) {

issue.getSubTaskObjects()?.findAll { it.issueType.name in ["Sub-task"]
def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key)
subtask.setAssigneeId(currentAssigneeIdValue)
ComponentAccessor.getIssueManager().updateIssue(cwdUser, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
Sai Krishna Yadav Challendula September 2, 2021

I'm getting below error. Please refer the logs.

Time (on server): Thu Sep 02 2021 15:07:10 GMT+0530 (India Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2021-09-02 11:37:10,045 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-09-02 11:37:10,045 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.currentAssigneeIdValue() is applicable for argument types: () values: []
 at Script885.run(Script885.groovy:13)
Tuncay Senturk
Community Champion
September 2, 2021

Yeah, of course. I did not change that part of your code.

I think below will work, bear in mind that I haven't run the codes, I am just trying to help you by coding in this window, sorry if you encounter any errors.

 

Issue issue = event.issue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "assignee"};
if (change) {
issue.getSubTaskObjects()?.findAll { it.issueType.name in ["Sub-task"]
def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key)
subtask.setAssigneeId(currentAssigneeIdValue)
ComponentAccessor.getIssueManager().updateIssue(issue.assignee, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
Sai Krishna Yadav Challendula September 2, 2021

Hi @Thanos Batagiannis [Adaptavist] ,

Can you please help me on this above script.

 

Thanks,

Sai

Tuncay Senturk
Community Champion
September 2, 2021

Hello @Sai Krishna Yadav Challendula 

What's the problem with my latest script? Has it not worked?

Sai Krishna Yadav Challendula September 2, 2021

it is not working.

Tuncay Senturk
Community Champion
September 2, 2021

Any error?

Sai Krishna Yadav Challendula September 2, 2021

I'm getting above error

Tuncay Senturk
Community Champion
September 2, 2021

I think you haven't checked my latest script. I already removed that part, you shouldn't get that error.

Can you try this one below?

Issue issue = event.issue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "assignee"};
if (change) {
issue.getSubTaskObjects()?.findAll { it.issueType.name in ["Sub-task"]}.each {
def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key)
subtask.setAssigneeId(currentAssigneeIdValue)
ComponentAccessor.getIssueManager().updateIssue(issue.assignee, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

 

Tuncay Senturk
Community Champion
September 2, 2021

If you get any error, please paste it rather than saying above, as there are lots of comments and it became hard to track.

Tuncay Senturk
Community Champion
September 7, 2021

Any update?

HARIOM MAKWANA January 5, 2023

Hi @Tuncay Senturk ,

I want to update the Component's field from Parent to Subtask. When I update parent Component's  field my Subtask Component fields gets change using Script Can you help me ???

Suggest an answer

Log in or Sign up to answer