Forums

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

Change description according to Combo using scriptrunner

Jens Jensen February 5, 2019

Hi.

I am new to scriptrunner but I am trying to make a "simple" script and I hope that someone can guide me in the right direction.

I have a custom field (Combo) where the user has to choose between some values. Empty, template1 and template2

When the field changes to template1 the issues description field changes its value to some predefined text etc.

After the change the user should be able to edit the field with additional values.

I believe that I should make it as a behavior on the Template field.

I have broken down the script to these steps

 

1. Trigger when the combo changes

2. Read the value from the combo 

3. Change the text in the description field.

 

Step one seems to happen by it self

Step two I have some code (dosn't work)

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParametersImpl
import org.apache.log4j.Level
import org.apache.log4j.Logger

def log = Logger.getLogger("Template logging")
log.setLevel(Level.DEBUG)

log.debug "Script started"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TemplateCf = customFieldManager.getCustomFieldObjectByName("Template")
log.debug "template:" + TemplateCf

 

The debug TemplateCf gives the name of the field, it seems. I need the value.

 

Then I hope I, with a little help of if value=="Template1" then change the description field.

But can I change the field this way, and how?

 

Hope someone have the time to help me.

 

Best regards,

Jens

 

5 answers

1 accepted

2 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2019

Hi @Jens Jensen,

 

you need to add this code to associated to Template CF (on scriptrunner side) in order to trigger this code on CF change.

FormField templateFF = getFieldByName("Template")
FormField descriptionFF = getFieldById("description");
String templateValue = templateFF.getValue();
if(templateValue!=null && templateValue.equalsIgnoreCase("Template1")){
descriptionFF.setFormValue("My description for Template1")
} else if(templateValue!=null && templateValue.equalsIgnoreCase("Template2")){
descriptionFF.setFormValue("My description for Template2")
}

 

Hope this helps.

Ciao,

Fabio

Hugo
Contributor
April 22, 2019

Thanks Fabio, you helped me with an issue.

1 vote
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2019

Hi @Jens Jensen,

 

you need to create a new behaviour (Add-ons -> Behavihours) and associate it to your project/issue type (use "Add Mapping"). Afetr doing that, you need to add your field (use "Fields" link) "Template" and put my code there.

 

image.png

0 votes
Jens Jensen February 6, 2019

Hi.

Thanks a lot for your answer.

I had to change a little bit before I got it to work.

FormField templateFF = getFieldByName("Template")
FormField descriptionFF = getFieldById("description");

to 

def templateFF = getFieldByName("Template")
def descriptionFF = getFieldById("description");

 

The complete working script looks like this

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

def log = Logger.getLogger("Change description logging")
log.setLevel(Level.DEBUG)

log.debug "Script started"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TemplateCf = customFieldManager.getCustomFieldObjectByName("Template")



def templateFF = getFieldByName("Template")
def descriptionFF = getFieldById("description");
log.debug "template:" + templateFF

String templateValue = templateFF.getValue();
if(templateValue!=null && templateValue.equalsIgnoreCase("Template 1")){
descriptionFF.setFormValue("My description for Template1")
} else if(templateValue!=null && templateValue.equalsIgnoreCase("Template 2")){
descriptionFF.setFormValue("My description for Template2")
} else if(templateValue!=null && templateValue.equalsIgnoreCase("Empty")){
descriptionFF.setFormValue("")
}

 

Just one small question that I hope you can answer.

The template I want to show in the description field needs some linebreaks etc. I have tried different things but I cant find a way to get it to work.

Do you know how to format a variable to show up in description with linebreaks etc.

 

Thanks

Best regards,

Jens

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

try using  \n fro the carriage return.

Please, accept my answer!

Ciao,

Fabio

0 votes
Jens Jensen February 6, 2019

Hi.

Thanks a lot.

It almost worked I had to make a minor adjustmen, but It might be me not knowing enough about scripts.

I had to change

FormField templateFF = getFieldByName("Template")
FormField descriptionFF = getFieldById("description");

to 

def templateFF = getFieldByName("Template")
def descriptionFF = getFieldById("description");

Here is the working script

import com.atlassian.jira.component.ComponentAccessor

import org.apache.log4j.Level
import org.apache.log4j.Logger

def log = Logger.getLogger("description change - logging")
log.setLevel(Level.DEBUG)

log.debug "Script started"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TemplateCf = customFieldManager.getCustomFieldObjectByName("Template")

 

def templateFF = getFieldByName("Template")
def descriptionFF = getFieldById("description");
log.debug "template:" + templateFF

String templateValue = templateFF.getValue();
if(templateValue!=null && templateValue.equalsIgnoreCase("Template 1")){
descriptionFF.setFormValue("My description for Template1")
} else if(templateValue!=null && templateValue.equalsIgnoreCase("Template 2")){
descriptionFF.setFormValue("My description for Template2")
} else if(templateValue!=null && templateValue.equalsIgnoreCase("Empty")){
descriptionFF.setFormValue("")
}

 

 

 

Just one small question to the end.

Do you know the right way to format the variable to linebreak etc. I have tried different ways but none of them seems to work.

 

Best regards,

Jens

0 votes
Jens Jensen February 5, 2019

Hi Fabio.

Thanks for your answer.

I understand your code but I am not sure where to put it

Can you please guide me?

Best regards,

Jens

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events