ScriptRunner Behaviours Constrained Issue get CustomField from current issue Edited

Hello,

i try to create a "constrained issue" (https://scriptrunner.adaptavist.com/5.2.0/jira/fragments/CreateConstrainedIssue.html) button for creating a new issue based on the current issue.

I get all fields that i need expect a single custom field value (single line text).

Here is the complete behaviours script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

ComponentManager componentManager = ComponentManager.getInstance()

def issueManager = ComponentAccessor.getIssueManager()
//Check to make sure this is the correct context for the behaviour.

if (getBehaviourContextId() == "create-document") {

//Set the project and issuetype to be readonly so the user cannot alter these.
getFieldById("project-field").setReadOnly(true)
getFieldById("issuetype-field").setReadOnly(true)

//Find the details of the Issue from which the request to link was made
def contextIssue = issueManager.getIssueObject(getContextIssueId())
def customFieldManager = ComponentAccessor.getCustomFieldManager()

//Pre-populate the Summary, issue link and issue link type, security level and reporter.
getFieldById("summary").setFormValue("Document Request created from ${contextIssue.reporter.displayName}").setReadOnly(false)
getFieldById(REPORTER).setFormValue("${contextIssue.reporterId}").setReadOnly(true)
getFieldById("security").setFormValue("${contextIssue.securityLevelId}").setReadOnly(true)
getFieldById("issuelinks-linktype").setFormValue("was split from").setReadOnly(true)
getFieldById("issuelinks-issues").setFormValue("${contextIssue.key}").setReadOnly(true)

// Hard Coded values work but i need the value from the current issue. In this case it's a single line text field
getFieldById("customfield_13206").setFormValue("Some hard coded textthat i want read from customfield_13206").setReadOnly(true)
}

 Here is the line that i want to make more dynamic :)

getFieldById("customfield_13206").setFormValue("Some hard coded textthat i want read from customfield_13206").setReadOnly(true)

Thanks for your help.

Regards,
Tim

1 answer

1 accepted

Hi Tim,

So if you are not in an issue create screen, you can get the issue using the 

underlyingIssue

bound variable. Therefore if you want to get the value of the custom field with key customfield_13206 you will need something like 

def cf = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_13206")
def cfValue = underlyingIssue.getCustomFieldValue(cf)

 Hope that helps, ping me if you have any other questions.

Regards, Thanos

Hello Thanos,

i've replaced my line with your line and this didn't work.

I get this error in atlassian-jira.log

2017-11-13 13:15:57,892 http-bio-8443-exec-6390 ERROR admin 795x1256291x1 1m0tvre 172.17.100.102 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validatorsByPid.json [c.o.jira.behaviours.BehaviourManagerImpl] *************************************************************************************
2017-11-13 13:15:57,894 http-bio-8443-exec-6390 ERROR admin 795x1256291x1 1m0tvre 172.17.100.102 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validatorsByPid.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: PS/Additional Documents, user: admin, fieldId: __init__, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
at Script1.run(Script1.groovy:41)

Regards,
Tim

Okay, i got it :) 

I think the problem was, that my variable for context was contextIssue and not underlyingIssue

 getFieldById("customfield_13206").setFormValue(contextIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_13206"))).setReadOnly(true) 

Thanks for your help.

Regards,
Tim

Hey Tim, 

Sorry I misread the question (the underlying issue would be the solution if you were editing the same issue).

But in your case what you are doing with the contextIssue is the right way.

Glad you found it :)

Regards, Thanos

Hi Tim,

Tried your code, but it doesn't work for me. Also tried with "${}" substitution such as:

getFieldById("customfield_13206").setFormValue("${contextIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_13206"))}").setReadOnly(true)

 That still didn't work. Did you put your code as Initialiser or on the Field itself?

Thanks,
Jeff

R I'm New Here Wednesday

Hey,

getBehaviourContextId() always returns null. Is there another method/field to get this context?

Regards.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,305 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot