Scriptrunner copy "user picker" value from "field A" in parent to "field B" in sub

Michael
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2024

Hi all,

I'm using the Post Function "Create Subtask (Scriptrunner)" to create a new subtask; but I need a way to copy a user picker value from a customfield within the parent ticket into the "Assignee" field within the subtask.

I'm trying to use the "Additonal Issue Actions" section to copy a user selected within "Field A" (customfield - User picker) into the "Assignee" field within the subtask.

So far I have this code:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def customFieldManager = ComponentAccessor.customFieldManager
def assigneeField = customFieldManager.getCustomFieldObjectsByName('Flowchart Assignee')
def parentIssue = issue.parentObject

issue.setAssignee(assigneeField, parentIssue.getCustomFieldValue(assigneeField))
issue.summary = issue.summary + " - Flowchart Task"

But I can't get it to work. Any help would be greatly appreciated.

Thanks,

Mike

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2024

Hi @Michael

Your code is not going to work as expected. In the code you have added in your description, you have included the @Behaviour annotation. This will not work for the Post-Function.

For your requirement, below is a sample working code:-

import com.atlassian.jira.user.ApplicationUser

if (issue.isSubTask()) {
def parent = issue.parentObject
def assignee = parent.getCustomFieldValue('Sample User Picker') as ApplicationUser

if (assignee) {
issue.set {
setAssignee(assignee.username)
}
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I have used ScriptRunner's HAPI features to simplify the code for this use case.

Below are the steps to configure the Post-Function:-

1. Go to your workflow, select the Create transition and click on the Post-Function link as shown in the screenshot below:-

config1.png2. Once you are in the Post-Function tab, click on the Add post function link as shown in the screenshot below:-

config2.png

3. Next, select the Custom script post-function [ScriptRunner] option as shown in the screenshot below:-

config3.png

4. Add the sample code into the code editor, as shown in the screenshot below. Save and publish the changes.

config4.png

Below are a couple of test screenshots for your reference:-

1. First, create an issue and select a user from the User Picker. Since this case requires the selected user to be set as the Assignee in the Sub-Task, use only the Single Select User Picker.

test1.png

2. Once the user is created, the option selected in the Sample User Picker field is visible as shown in the screenshot below:- test2.png

3. Finally, create a Sub-Task. Once the sub-task is created, as expected, the User selected in the User Picker of the Parent Issue is set as the Assignee in the Sub-Task.

test3.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

Michael
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2024

Thank you Ram,

I've used a combination of your code above and Pablo's code to get this working:

import com.atlassian.jira.user.ApplicationUser

def parentIssue = issue.parentObject
def assigneeField = parentIssue.getCustomFieldValue('Flowchart Assignee') as ApplicationUser

//Copies user picker field value defined in 'assigneeField' and adds it into the subtask's 'Assignee' field
issue.set {
setAssignee(assigneeField.username)
}

This worked perfectly for our needs.

Have a great day ahead,

Mike

1 vote
Pablo Vergara March 20, 2024

Hi @Michael 

While I don't directly use or support ScriptRunner, I can help you with the Groovy piece of this requirement.

Basically, you need to retrieve the Jira user object from the parent issue (I assume it would be the current issue that has triggered the event or transition) using the Groovy snippet below.


import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def parentIssue = issue
def assigneeField = customFieldManager.getCustomFieldObject("customfield_XXXXX").getValue(parentIssue)

// Then, at this point of the script, you just need to use the proper ScriptRunner routine to update the assignee field value.


(Don't forget to replace the customfield_XXXXX with your own user picker custom field id)

I see the field update part is explained on the ScriptRunner documentation here: https://docs.adaptavist.com/sr4js/latest/hapi/update-fields

I hope this helps you!

- Pablo / ServiceRocket SSE

Michael
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2024

Thank you Pablo,

I used a combination of your code and Ram's above to get this to work within the "Create Subtask" postfunction.

I'll post the final script within a Reply to Ram's comments above.

Have a great day,

Mike

Like Pablo Vergara likes this

Suggest an answer

Log in or Sign up to answer