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

Need to display options set in a field in Jira to a different value in a new text field Edited

Hello Community, 

Can someone please advise changing the piece of code below to covert the set option selecting from a field in Jira to a different value and display in a new field separated by comma? I have Field1 with a bunch of options already set in Jira but I have created Field2 as a text form that I want to display all options as defined in Def from C1-C3 which equals to each options in Field1. My below code can give the same behavior I want, but I only can display the option from Field1 but not from of component IDs below. 

Example: if the list of options in Field1 Jira is 1, 2 &3, when select 1 &2, Field 2 should display Cat, Dog. If select 2 & 3, Field 2 will display Dog, Rat.

Thank you in advance for your kind help. Looking forward to your answers. 

 

import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript
import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.core.util.StringUtils

@BaseScript FieldBehaviours fieldBehavioursdef

optionsManager = ComponentAccessor.getOptionsManager()// Get the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// Get a pointer to my select list custom fields
def selectList1 = getFieldByName("Field1")
def selectList2 = getFieldByName("Field2")


// Set List of Component IDs
def C1 = "Cat"
def C2 = "Dog"
def C3 = "Rat"


def selectList1Val = selectList1.getValue()

if (selectList1 != null)
{
selectList2.setFormValue(selectList1.getValue())
selectList2.setReadOnly(true)
}
//All other selections
else
{

selectList2.setReadOnly(false)
selectList2.setHidden(false)
}

1 answer

1 accepted

0 votes
Answer accepted
Antoine_Berry Community Leader Jan 06, 2020

Hi @Kay ,

If I understood correctly your Field2 is a text field. You could try to use this code snippet : 

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours

@BaseScript FieldBehaviours fieldBehavioursdef


// Get a pointer to my select list custom fields
def selectList = getFieldByName("Field1")
def textField = getFieldByName("Field2")

def map = ["C1":"Cat ","C2":"Dog ","C3":"Rat "]


def selectListValue = selectList.getValue()

String textFieldValue = ""
for (selectValue in selectListValue) {
textFieldValue += (map[selectValue] ? map[selectValue] : "")
}

if (textFieldValue != ""){
textField.setFormValue(textFieldValue)
textField.setReadOnly(true)
}
else {
selectList2.setReadOnly(false)
}

Let me know if that helped.

Antoine

@Antoine_Berry 

Happy New Year! Thank you for your code. I have added the following code, but still Field 2 doesn't pull up the value from map. 

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours

@BaseScript FieldBehaviours fieldBehavioursdef


// Get a pointer to my select list custom fields
def selectList = getFieldByName("Field1")
def textField = getFieldByName("Field2")

def map = ["C1":"Cat ","C2":"Dog ","C3":"Rat "]


def selectListValue = selectList.getValue()

String textFieldValue = ""
for (selectValue in selectListValue) {
textFieldValue += (map[selectValue] ? map[selectValue] : "")
}

if (textFieldValue != ""){
textField.setFormValue(textFieldValue)
textField.setReadOnly(true)
}
else {
textField.setReadOnly(false)
}

@Antoine_Berry  I have the code below that I can assign values to Field1 when selecting values in Field 1 but I can select the value one by one instead of all or a few values to show up in Field 2. 

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours

@BaseScript FieldBehaviours fieldBehavioursdef


// Get a pointer to my select list custom fields
def selectList = getFieldByName("Field1")
def textField = getFieldByName("Field2")

def C1 = "Cat"
def C2 = "Dog"
def C3 = "Rat" 


def selectListValue = selectList.getValue()

textField.setFormValue(C1)
 textField.setReadOnly(true)
// Next Value
} else if ("1" in selectList.value)
{
textField.setFormValue(C2)
 textField.setReadOnly(true)
// Next Value
} else if ("2" in selectList.value)
{
textField.setFormValue(C3)
 textField.setReadOnly(true)
// Next Value
} else if ("3" in selectList.value)

I got it working now by changing the map key to match exactly the values in Field1. I also changed the for loop a bit. Thank you Antoine. 

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours

@BaseScript FieldBehaviours fieldBehavioursdef


// Get a pointer to my select list custom fields
def selectList = getFieldByName("Field1")
def textField = getFieldByName("Field2")

def map = ["1":"Cat ","2":"Dog ","3":"Rat "]


def selectListValue = selectList.getValue()

String textFieldValue = ""
for (selectValue in selectListValue) {
 textFieldValue += ("${map[selectValue]}, " ? "${map[selectValue]}, ": "")
}

if (textFieldValue != ""){
textField.setFormValue(textFieldValue)
textField.setReadOnly(true)
}
else {
textField.setReadOnly(false)
}
Antoine_Berry Community Leader Jan 06, 2020

Thanks, happy new year too @Kay !

Glad you have figured it out, good job. Indeed you need to be careful when using Strings, they have to be matching exactly. :)

Suggest an answer

Log in or Sign up to answer
TAGS

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