Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How populate template in description base on issue type?

I'm trying to add about three templates for three issue types, such as the "Story", "Task" and "Bug" issue types. When the user selects Story it adds story template or Bug then Bug template. The following is my behavior script but it's not working.

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def issueType = getFieldById("issueType")

def desc = getFieldById("description")

def taskValue = """
h2. Please fill out the below form
* Team Name:
* Start Date:
*End Date:
* Project(s):
* Activities:

""".stripIndent()

def storyValue = """
h2. Please fill out the below form
* Server:
* Access
* Due Date:
* Job Perform:
""".stripIndent()

def bugValue = """
h2. Please fill out the below form
* Error Code:
* First Reported:
* Whoe Reported:
* Action Taken:
* Access Start Date:
* Estimated Resolution Date:
* Additional Info:

""".stripIndent()

if (issueType.value == "Task") {
desc.setFormValue(taskValue)
}

if (issueType.value == "Story") {
desc.setFormValue(storyValue)
}

if (issueType.value == "Bug") {
desc.setFormValue(bugValue)
}

It does not add a template to the description box. I know there is something wrong with the code but I couldn't figure it out.

Thank you for your help

1 answer

1 vote

First, there is no such field with id "issueType"

You can try "issuetype" but that will be a id (integer or long) of the issue type.

Generally, I prefer to get my issue type from the issueContext variable:

if(issueContext.issueType.name == "Task"){
//your code here
}

Also, I'm a fan of using maps as data structures... try this

def issueTypeMap =[
Task: """\
h2. Please fill out the below form
* Team Name:
* Start Date:
* End Date:
* Project(s):
* Activities:
""",
Story: """\
h2. Please fill out the below form
* Server:
* Access
* Due Date:
* Job Perform:
""",
Bug: """\
h2. Please fill out the below form
* Error Code:
* First Reported:
* Whoe Reported:
* Action Taken:
* Access Start Date:
* Estimated Resolution Date:
* Additional Info:
"""
]

if(actionName == 'Create Issue'){
def desc = getFieldById("description")
def issueType = issueContext.issueType.name
if(issueTypeMap.containsKey(issueType)){
desc.setFormValue(issueTypeMap[issueType].stripIndent())
} else {
if(desc.value in issueTypeMap*.value*.stripIndent()){
// reset the value in case an issuetype not in our map is selected,
// but only if the description is one of our defaults
desc.setFormValue("")
}
}
}

I copied pasted your code, however, for some reason, it is not adding a template in the description box. I am mapping the script to the Description field. What can be wrong? I appreciate your help.

This needs to be in the "initializer" server-side script

The "description" server-side script only executes when the user changes the description field.

For some reason, it is not working. Maybe I am doing something wrong. I ran the code in the console and got some errors too. I'm attaching screenshots that might help. Thank you for your help.

ScriptRunner_07192021_01.jpgScriptRunner_07192021_02.jpgScriptRunner_07192021_03.JPG

Behaviour scripts can't run in the console. Methods like getFiedById(), getFieldByName(), getFieldChange(), getActionname() etc don't exist in the console.

You can try to make some adjustments like this to get some clues are to what's not working:

def issueTypeMap =[
Task: """\
h2. Please fill out the below form
* Team Name:
* Start Date:
* End Date:
* Project(s):
* Activities:
""",
Story: """\
h2. Please fill out the below form
* Server:
* Access
* Due Date:
* Job Perform:
""",
Bug: """\
h2. Please fill out the below form
* Error Code:
* First Reported:
* Whoe Reported:
* Action Taken:
* Access Start Date:
* Estimated Resolution Date:
* Additional Info:
"""
]

def desc = getFieldById("description")
def issueType = issueContext.issueType.name

def debugText = "Description field found. <br>ActionName = $actionName<br>issueType = $issueType"

if(actionName == 'Create Issue'){
debugText += "<br>Create Issue actionName found"
if(issueTypeMap.containsKey(issueType)){
debugText += "<br>Correct issueType was found. Setting default desc value"
desc.setFormValue(issueTypeMap[issueType].stripIndent())
} else {
debugText += "<br>Incorrect issueType was found. Test if we can reset the desc"
if(desc.value in issueTypeMap*.value*.stripIndent()){
// reset the value in case an issuetype not in our map is selected,
// but only if the description is one of our defaults
debugText += "<br>Current description matches a template. Resetting to blank"
desc.setFormValue("")

} else {
debugText += "<br>Current description doesn't match a template. Leave it as is (user input)"
}
}
} else {
debugText += "<br>Create Issue actionName NOT found"
}
desc.setHelpText(debugText)

 If you don't see any red help text under the description field, then you will need to look at the atlassian-jira.log file under your jira installation directory. There may be additional clues.

@Peter-Dave SheehanI am seen create issue not found below the description field. The screenshot is attached. Thank you

ScriptRunner_07192021_04.JPG

Ok, then change this line:

if(actionName == 'Create Issue'){

to

if(actionName == 'Create'){

Not sure why I assumed the actionName would be "Create Issue" ... even in my own code, I use "Create" when checking which actionName to execute some behaviour script against.

But if you don't think you could have figured this out by yourself, you probably need to study the code a little closer to make sure you understand it enough to be able to support it in your environment.

Like Shah Baloch likes this

@Peter-Dave Sheehan, it worked, thank you so much for helping me out.

Hi @Peter-Dave Sheehan

Sorry to bother you. My project requirement got changed. The team wants to display a template base on a custom field selection. For example, there is a custom drop-down field "Environment" with options "Production", "Development", "Test", when the user selects "Production" from the custom field it should display a template if selects "Development" or "Test" it should display other templates. How I can make it work with a custom field instead of an issue type? I couldn't figure it out. Thank you for your help.

What have you tried... what were the results?

Did you try to output intermediate values to log to try to investigate this yourself?

Hi @Peter-Dave Sheehan

That time "value" was missing, I added "environement.value" and it worked. I'm not sure this is the right way to create the script. I created a behavirour script. Here is working code, if you have a better suggestion.

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def environement = getFieldById('customfield_17800')
def desc = getFieldById("description")

def prodValue = """
h2. Please fill out the below form
* Team Name:
* Start Date:
* End Date:
* Project(s):
* Activities:

""".stripIndent()

def devValue = """
h2. Please fill out the below form
* Server:
* Access
* Due Date:
* Job Perform:
""".stripIndent()

def testValue = """
h2. Please fill out the below form
* Error Code:
* First Reported:
* Whoe Reported:
* Action Taken:
* Estimated Resolution Date:
* Additional Info:

""".stripIndent()

if(environement.value == "Production") {
desc.setFormValue(prodValue)
}
if(environement.value == "Development") {
desc.setFormValue(devValue)
}
if(environement.value == "Test") {
desc.setFormValue(testValue)
}

Thank you

If that works for you then it's perfect.

Yes, "environment" is a "FormField" object. You can get the value selected by the user with evironment.value (this is shortcut for environment.getValue()).

I see that you don't have the "actionName" check anymore. Be careful with that. If you later change the environment field, the next time you open the edit form, you description will be erased and replaced by the template.

How can I prevent erasing the description of existing tickets if users want to edit any of the tickets after implementing the templates? Thanks

Try this code:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def environement = getFieldById('customfield_17800')
def desc = getFieldById("description")

def prodValue = """
h2. Please fill out the below form
* Team Name:
* Start Date:
* End Date:
* Project(s):
* Activities:

""".stripIndent()

def devValue = """
h2. Please fill out the below form
* Server:
* Access
* Due Date:
* Job Perform:
""".stripIndent()

def testValue = """
h2. Please fill out the below form
* Error Code:
* First Reported:
* Whoe Reported:
* Action Taken:
* Estimated Resolution Date:
* Additional Info:

""".stripIndent()

if(!underlyingIssue){
if(environement.value == "Production") {
desc.setFormValue(prodValue)
}
if(environement.value == "Development") {
desc.setFormValue(devValue)
}
if(environement.value == "Test") {
desc.setFormValue(testValue)
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

🌐 BigPicture-Marketplace Apps Atlympics

Hello Atlympics, here's a market place app that's a game changer given the features and excellent support.  Link to the tool:  Bigpicture project management ppm plugin How did ...

30 views 0 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you