Forums

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

How the Script runner Behavior gets the value of the parent task field

jack ma August 12, 2021

Hi 

I want to get the parent task field value through Script runner Behavior.  But the acquisition method seems to be different from the Post function. 

Thanks for your help.

Best Regards.

tu_09138746501.pngtu_091381255847.png

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 13, 2021

Hi @jack ma

If you want to pass a value from an Issue to a Sub-Task, you could try something like this:-

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

def cost = getFieldByName("Cost")
def customFieldManager = ComponentAccessor.customFieldManager
def costUpdate = customFieldManager.getCustomFieldObjectsByName("Cost")[0]

@BaseScript FieldBehaviours behaviours
if (underlyingIssue != null && underlyingIssue.isSubTask()) {
def parent = underlyingIssue.parentObject
cost.setFormValue(parent.getCustomFieldValue(costUpdate))
}

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

The code will not work on the Sub-Task's Create screen. It will only trigger once the Sub-Task is created and the Edit Screen is used.

Alternatively, if you want to see the result immediately after the Sub-Task has been created, it would be better to use the Scripted Field.

I hope this helps to answer your question. :)

Thank you and Kind Regards,
Ram

jack ma August 15, 2021

Hi@Ram Kumar Aravindakshan _Adaptavist_ 

I have tried your method, but the result of the underlyingIssue is null.  The scriptrunner version I use is 6.3.0-p5.

Thank you and Best Regards

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 16, 2021

Hi @jack ma

The ScriptRunner version you are using is pretty outdated, and there have been many bug fixes since that release.

If you are using Jira 8.0 and above, you can upgrade the ScriptRunner plugin to the latest release, i.e. 6.32.0.

The test I have done is using the latest release of ScriptRunner.

Thank you and Kind Regards,

Ram

0 votes
Fabian
Contributor
August 12, 2021

@jack ma 

i tried this in a test behaviour and both is working. I hope this is it what you are searching for:

Unbenannt.PNG

Kind regards.

jack ma August 12, 2021

Hi@Fabian Laqua 

I really appreciate your help.

After I used your code to execute it, an error was reported. The error message is as follows:

tu_141382535885.pngtu_141383044827.png

Best regards.

Fabian
Contributor
August 13, 2021

Dear @jack ma ,

what scriptrunner version do you use? 

In an behaviour, you get the current issue with the issueContext

Plz try, if it is empty or not? 

This works too (Full code, no import needed in a behaviour, initialiser!):

log.warn("DEBUG now:")
log.warn(issueContext)
log.warn(getIssueContext())

Source: How to get current issue object in Behaviour ? (atlassian.com)

How i did my example:

1. Created a behaviour

2. mapped it to my jira project and subtask

3. created an Initialiser

4. wrote my example.

 

If it doesn`t work, try out getFieldById(<Issue key field>) and then you can use the Issuemanager to get the issue like in my example above.

jack ma August 15, 2021

Hi@Fabian Laqua

The script version I use is 6.3.0-p5  Jira version is v8.0.2

tu_141682320390.pngtu_141682149241.pngtu_141682204114.png

I’m on the issue creation interface, and I haven’t finished creating it yet, I want to get the value of the parent task field when creating a child task. so I can’t use the getFieldById() method to get the value of issue key.

Thank you and Best Regards.

Fabian
Contributor
August 16, 2021

Dear @jack ma 

now i understand your problem. There isnt a sub-issue yet. So you cannot cast to mutableIssue.

Why do you need the parent issue in the create mask?

Is there a special field you need to display or process?

If you need to display that data, then you can use a post function in the sub-taks-create transition to get that field.

Or do you need to recalculate something with this field in the create mask?

And that underlying wont work either in the create transition. I tried it out. But if you that sub-issue in edit the "underlying" works.

Fabian
Contributor
August 16, 2021

@jack ma 

This works only in the create transition:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.jira.groovy.user.FormField

IssueManager im = ComponentAccessor.getIssueManager();

FormField formfieldparent = getFieldById("parentIssueId")
Long parentIssueId = formfieldparent.getFormValue() as Long

log.warn("TEST formfieldparent: " + formfieldparent)
log.warn("TEST parentIssueId: " + parentIssueId)

MutableIssue issue = im.getIssueObject(parentIssueId);
log.warn("FINISHED: " + issue)

With that parentIssueId, you can get the parent issue with the issueManager i posted at first.

 

Kind regards.

Suggest an answer

Log in or Sign up to answer