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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
try using \n fro the carriage return.
Please, accept my answer!
Ciao,
Fabio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.