Forums

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

How to auto update select list custom field(valueList) based on other custom field value selection?

Cheerla Vamsi Krishna
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!
March 15, 2022

Hi Team,

I am trying to update select list custom field Value( List) based on other custom field value selection using listeners in scrip runner.

For Example: If i select Custom field A and the  Value is B Then Custom field C Value should be updated as D by using the Listeners in scrip runner.

Can any one help on the same.

Thanks,
CH Vamsi

3 answers

0 votes
Cheerla Vamsi Krishna
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!
March 18, 2022

Hi @Piyush A (STR)  I have used the below code to set the value in one of the custom field but not able to set. can you please help me on the same.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("HSYS-5318")

if (issue){

//Get the value
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_XXX")
def cFieldvalue = issue.getCustomFieldValue(cField)

if ( cFieldvalue != "Devices"){

def customFieldManagerPL = ComponentAccessor.getCustomFieldManager()
def cFieldPL = customFieldManagerPL.getCustomFieldObject("customfield_XXX")
def cFieldvaluePL = issue.getCustomFieldValue(cFieldPL)

log.warn("Current parent Link ---" + cFieldvaluePL)
final customFieldName = 'Parent Link'
final isu = 'HSYS-5318'
final newValue = 'SFLS-889 CC-ART: Tender Insights within Portfolio'
def issu = ComponentAccessor.issueManager.getIssueByCurrentKey(isu)
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issu).findByName(customFieldName)
def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issu))
def optionToSet = availableOptions.find { it.value == newValue }
customField.updateValue(null, issu, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
//cFieldvaluePL.updateValue(null, issue , new ModifiedValue(cFieldValue, 'Test'))
//cFieldPL.updateValue(null, issue, new ModifiedValue(cFieldvaluePL, "new DefaultIssueChangeHolder"))

}
//Update the Value
//def changeHolder = new DefaultIssueChangeHolder()
//cField.updateValue(null, issue new ModifiedValue(cFieldValue, "Test"))
log.warn("Portfolio value is ---- " +cFieldvalue)
}else {
return "Issue doesn't exist"
}

0 votes
Piyush A (STR)
Community Champion
March 16, 2022

Hi @Cheerla Vamsi Krishna 

Welcome to the community. !!

 

Add the below code to the listeners for Issue Update and Create. You may try to add new conditions or changes according to your needs.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue

// the name of the custom field (single select list type)
final customFieldNameA = 'Select List CF'
final customFieldNameB = 'Select List CF B'

// the value of the new option to set
final newValue = 'D'
final exValue = 'A'
Issue issue = event.issue
def issueKey = issue.key
log.warn(issueKey)
assert issue: "Could not find issue with key $issueKey"

def customFieldA = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(customFieldNameA)
assert customFieldA: "Could not find custom field with name $customFieldNameA"

def customFieldB = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(customFieldNameB)
assert customFieldB: "Could not find custom field with name $customFieldNameB"

if(issue.getCustomFieldValue(customFieldA).toString() == 'A' && issue.getCustomFieldValue(customFieldB) == null ){
def availableOptions = ComponentAccessor.optionsManager.getOptions(customFieldB.getRelevantConfig(issue))
def optionToSet = availableOptions.find { it.value == newValue }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"
customFieldB.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customFieldB), optionToSet), new DefaultIssueChangeHolder())
}

 

Also, on the create screen itself you can make the auto changes using Behaviour.

kiranmai_genkolla March 21, 2022

Hi @Piyush A (STR) ,

 

In my case the parent link in the portfolio custom field .. can any one help ime seting the value in this custom field .with REST API 

kiranmai_genkolla March 21, 2022

Please help me in code for this 

Piyush A (STR)
Community Champion
March 21, 2022

With Rest api? OR via the scriptrunner?

kiranmai_genkolla March 21, 2022

script listner (with rest api in it)

0 votes
Tansu Akdeniz
Community Champion
March 15, 2022

Hi @Cheerla Vamsi Krishna 

Welcome to the community.

Do you have Automation for Jira add-on? If yes, you can simply define a rule for this scenario. For sure, it is possible via Script Runner. But you should be familiar with scripting. You can look at sample codes in community.

Also take a look at: https://scriptrunner.adaptavist.com/latest/jira/listeners.html

Suggest an answer

Log in or Sign up to answer