How to copy field from parent to subtask on create screen using behaviour

Rob B
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.
October 13, 2023

I want to be use a behaviour to copy multiple fields from the parent to the subtask so they appear before i create the subtask.

I have figured out how to do the summary using other posts on the community, but i cant get the custom fields to work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

import java.sql.Timestamp

import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long


if (!parentIssueId || field.getFormValue()) {
// this is not a subtask, or the field already has data
return
}

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfspid = getFieldByName("SPID")

// REMOVE OR MODIFY THE SETTING OF THESE FIELDS AS NECESSARY
getFieldById(SUMMARY).setFormValue(parentIssue.summary)
//getFieldById(SPID).setFormValue(field.cfspid)

 

1 answer

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.
October 13, 2023

Hi @Rob B

You cannot get the values from the Parent issue field and pass them to the Sub-Task during issue creation. Why? This is because the issue (Sub-task) doesn't exist yet. Hence, the link between the Parent issue and the Sub-task doesn't exit, so you cannot invoke the value from the Parent issue.

To proceed,  I suggest you first create an Issue Picker scripted field so you can select the Parent issue from it.

Once the Parent issue is selected from the Issue Picker, you can invoke the values from the system field as well as the custom field(s) from the selected Parent issue and pass it to the Sub-task on the Create screen.

Below is a screenshot of the Issue Picker configuration:-

issue_picker.png

Next, you must create a Server-Side Behavior for the Issue Picker field.

Below is the sample working code for your reference:-

import com.adaptavist.hapi.jira.issues.Issues
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def issuePicker = getFieldById(fieldChanged)
def issuePickerValue = issuePicker.value.toString()

def sampleMultiLine = getFieldByName('Sample Multi Line')
def sampleNumber = getFieldByName('Sample Number')

if (issuePickerValue == 'null') {
return
}

def
parentIssue = Issues.getByKey(issuePickerValue)
def parentMultiLineValue = parentIssue.getCustomFieldValue('Sample Multi Line')
def parentNumberValue = parentIssue.getCustomFieldValue('Sample Number')
sampleMultiLine.setFormValue(parentMultiLineValue)
sampleNumber.setFormValue(parentNumberValue)

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

Below is a screenshot of the Issue Picker Behaviour configuration:-

behaviour_config.png

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

1. Below is a screenshot of the Parent issue. In it, 2 custom fields have been set with a value, i.e. Sample Multi Line and Sample Number.

test1.png

 

2. Next, with the Sub-task's Create screen, no values are passed into the Sample Multi Line and Sample Number fields as shown below:-test2.png

 

3. Finally, once the Parent issue has been selected from the Issue Picker, the values from the custom fields are passed to the Sub-task as shown in the screenshot below:- test3.png

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

Thank you and Kind regards,

Ram

Rob B
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.
October 13, 2023

Hi Ram, 

Thanks for the reply. We only have scriptrunner version 6.5.0. Can i still do this?

Thanks

Rob

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.
October 13, 2023

Hi @Rob B

To answer your question, yes you can.

But the approach to invoke the parent Issue is via the ComponentAccessor.issueManager.

Can you please clarify what is the current version of Jira you are using? Is it 7.13.1? 

I'm asking this to see if you can upgrade your ScriptRunner plugin to a version that has the HAPI feature so you can use the same approach that I have provided in the example.

If you are using version 7.13.1, the HAPI approach will not be applicable to you.

Instead, you will need to use the longer more cluttered approach:-

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

@BaseScript FieldBehaviours behaviours
def issuePicker = getFieldById(fieldChanged)
def issuePickerValue = issuePicker.value.toString()

def sampleMultiLine = getFieldByName('Sample Multi Line')
def sampleNumber = getFieldByName('Sample Number')

if (issuePickerValue == 'null') {
return
}

def issueManager = ComponentAccess.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def
parentIssue = issueManager.getIssueByCurrentKey(issuePickerValue)

def parentMultiLine = customFieldManager.getCustomFieldObjectsByName('Sample Multi Line').first()
def parentMultiLineValue = parentIssue.getCustomFieldValue(parentMultiLine) as String

def parentNumber = customFieldManager.getCustomFieldObjectsByName('Sample Number').first()
def parentNumberValue = parentIssue.getCustomFieldValue(parentNumber)

sampleMultiLine.setFormValue(parentMultiLineValue)
sampleNumber.setFormValue(parentNumberValue)

Thank you and Kind regards,

Ram

Rob B
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.
November 7, 2023

v7.13.1

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.1
TAGS
AUG Leaders

Atlassian Community Events