Forums

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

Scriptrunner - Edit Description based on Component

Jon Langshaw
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 16, 2022

Can someone review and let me know why this wont work?

import com.atlassian.jira.bc.project.component.ProjectComponent

def desc = getFieldById("description")

def component = getFieldById(getFieldChanged()) 

def defaultValue = """

*WHAT:* chicken, chicken chicken

*WHY*

""".replaceAll(/    /, '')

 

for(c in comps){

     if(c.getName().contains(“Stuff”)){defaultValue = true}

}

if (! underlyingIssue?.description) {

desc.setFormValue(defaultValue)

}

1 answer

0 votes
PD Sheehan
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.
February 16, 2022
  1. Where is "comps" on the line "for (c in comps)" coming from?
    That variable is not defined anywhere.
  2. Suff appears to be surrounded by invalid quotes. Maybe this happened when you copied here. Or maybe this happened because you edited the script in a word processor rather than a code editor. Make sure you use either ' or " and not ” (notice the slant on the last one?
  3. You set the variable "defaultValue" to the boolean "true" if c (whatever c is ) has a name that contains stuff. Then you try to set the description to a boolean. The best-case scenario, this will be converted to a string "true" or it might fail altogether.

Perhaps the following will achieve something close to what you expect:

import com.atlassian.jira.bc.project.component.ProjectComponent

def defaultValue = """
*WHAT:* chicken, chicken chicken

*WHY*

""".stripIndent()

if (! underlyingIssue?.description) {
def components = getFieldById(fieldChanged) as List<ProjectComponent>
if(components.any{it.name.contains('Stuff')}){
def desc = getFieldById("description")
desc.setFormValue(defaultValue)
}
}
Inayat Nahvi
Contributor
October 19, 2023

Hi @PD Sheehan .   I tried this code.  It compiles with no issues, but it does not seem to populate the Description.   In the behavior, I have it set to the Component/s field, not the initializer.

If the component of 'Stuff' is selected on the create screen, it should add the defaultValue to the description box, right?

PD Sheehan
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 19, 2023

Correct, that's what this code is designed to do.

If that's not working for you ( and you are on a server/dc instance) you will need to add some logging and review your jira logs to see what might be happening.

For example:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.apache.log4j.Level
log.setLevel(Level.INFO) // make sure log level is adequate to view the messages

log.info "Start of behaviour for Component/s field"
def defaultValue = """
*WHAT:* chicken, chicken chicken

*WHY*

""".stripIndent()

if (! underlyingIssue?.description) {
def components = getFieldById(fieldChanged).value as List<ProjectComponent> //I see the original code was missing .value
log.info "component/s field contains ${components.size()} values, checking if any of them contain stuff"
if(components.any{log.info("component name=$it.name");it.name.contains('Stuff')}){
log.info "stuff component found, setting the description field"
def desc = getFieldById("description")
desc.setFormValue(defaultValue)
}
} else {
log.info "an existing issue was detected ($underlyingIssue.key), we won't update the description"
}
log.info "end of behaviour for Component/s field
Like # people like this
Inayat Nahvi
Contributor
October 19, 2023

Thanks @PD Sheehan .  This is working now.  I did have to make one correction and change:

 

import com.apache.log4j.Level

to

import org.apache.log4j.Level

Suggest an answer

Log in or Sign up to answer